printf

Reuse of va_list

蹲街弑〆低调 提交于 2020-05-14 16:59:29
问题 I need to do two (or more) passes over a va_list . I have a buffer of some size, and I want to write a formatted string with sprintf into it. If the formatted string doesn't fit in the allocated space I want to double the allocated space and repeat until it fits. (As a side-note, i would like be able to calculate the length of the formatted string first and allocate enough space, but the only function that I found that can do that is _snprintf, and it is deprecated in VS2005 ...) Now, so far

Understanding printf better - What does it print with “%c” when the value provided is negative?

两盒软妹~` 提交于 2020-05-09 07:33:47
问题 In Kernighan & Ritchie, it says that "all printable characters are positive when though char datatype being signed or unsigned is machine-dependent." Can somebody explain to me the meaning of this line ? My system has signed chars but even with a negative value say of -90, printf does print a character (even though its not a very familiar character). 回答1: ASCII character set defines codepoints from 0x00 to 0x7F . It doesn't matter if they are represented with unsigned or signed byte values

C print hex bytes

对着背影说爱祢 提交于 2020-05-02 04:22:48
问题 I have code in a C program with hex data which I want to iterate over, for example to print out each of the hex bytes: char input[] = "\x31\xd2\xb2\x30"; for (int i = 0; i < strlen(input); i++) { printf("%02X\n", input[i]); } However, the output is not what I expect, for example the above prints: 31 FFFFFFD2 FFFFFFB2 30 I also tried to cast the output as an (unsigned int) , however I receive the same output. Can somebody point out the issue with this simple script? 回答1: The arguments passed

C print hex bytes

萝らか妹 提交于 2020-05-02 04:21:26
问题 I have code in a C program with hex data which I want to iterate over, for example to print out each of the hex bytes: char input[] = "\x31\xd2\xb2\x30"; for (int i = 0; i < strlen(input); i++) { printf("%02X\n", input[i]); } However, the output is not what I expect, for example the above prints: 31 FFFFFFD2 FFFFFFB2 30 I also tried to cast the output as an (unsigned int) , however I receive the same output. Can somebody point out the issue with this simple script? 回答1: The arguments passed

Varadict functions

我是研究僧i 提交于 2020-04-30 06:24:09
问题 How should I set my_printf, so it would do what printf("%p") does + without using printf. void my_printf(char * format, ...) { va_list ap; va_start(ap, format); if(!strcmp(format,"%p")) { void *address= va_arg (ap, void*); char *arr=malloc(sizeof(address)); arr=address; arr[strlen(arr)]='\0'; write(1,arr,strlen(arr)); } va_end (ap); //it has to print an address in hexadecimal format. } 回答1: In : char *arr=malloc(sizeof(address)); arr=address; the allocated block is lost, this is a memory leak

Why is © (the copyright symbol) replaced with (C) when using wprintf?

两盒软妹~` 提交于 2020-04-10 09:09:47
问题 When I try to print the copyright symbol © with printf or write , it works just fine: #include <stdio.h> int main(void) { printf("©\n"); } #include <unistd.h> int main(void) { write(1, "©\n", 3); } Output: © But when I try to print it with wprintf , I get (C) : #include <stdio.h> #include <wchar.h> int main(void) { wprintf(L"©\n"); } Output: (C) It's fixed when I add a call to setlocale , though: #include <stdio.h> #include <wchar.h> #include <locale.h> int main(void) { setlocale(LC_ALL, "");

Why is © (the copyright symbol) replaced with (C) when using wprintf?

有些话、适合烂在心里 提交于 2020-04-10 09:05:56
问题 When I try to print the copyright symbol © with printf or write , it works just fine: #include <stdio.h> int main(void) { printf("©\n"); } #include <unistd.h> int main(void) { write(1, "©\n", 3); } Output: © But when I try to print it with wprintf , I get (C) : #include <stdio.h> #include <wchar.h> int main(void) { wprintf(L"©\n"); } Output: (C) It's fixed when I add a call to setlocale , though: #include <stdio.h> #include <wchar.h> #include <locale.h> int main(void) { setlocale(LC_ALL, "");

洛谷 P2934 安全路径

倖福魔咒の 提交于 2020-04-08 00:48:21
咕咕咕咕咕咕咕咕咕咕咕咕咕咕咕咕咕咕咕咕咕咕咕 洛谷 P2934 安全路径 KONO题面哒……然而并没有完整题面 题目大意: 给出一个N节点、M条边的无向图,求出到每个节点的 安全路径 的最小长度。 安全路径 定义:不经过1号点到当前节点的最短路的最后一条边的路径。 数据范围: \(N\leq10^5,M\leq2*10^5\) 提供可并堆(中的左偏树) 因为只学了左偏树 的做法。 吐槽,题面中样例说明的迫真连通图稍微有那么一点点难看。 当然,要是解析出了题目的要点,这道题和样例说明还是挺好理解的。但做法稍微有点绕。 首先我们想到把整个连通图浓缩成一个 最短路径树 (即每条边都位于原图最短路上的树)。 然后原连通图就会变成介个样子。 那么,我们把原图缩成最短路径树有什么用呢?由于安全路径的定义,所以在寻找到一个节点的安全路径时,肯定有一条非树边连接在那个节点上。而又由于要求长度最小,所以整个安全路径只存在一条非树边。这样的话就好展开情况讨论了。 补:安全路径并非次短路,因为次短路也有可能经过不能通过的那条边。 (由于画的丑 而且懒 ,就不补图了) 目标点(A)为叶子结点——找到所有以A为端点的线和与A相连的点,答案就为 \(min(dis[x]+d)\) )(d为边权) \(Ans=min(dis[x]+d+dis[y])-dis[A]\) (此情况下 \(A=y\) )

STL标准模板库

只愿长相守 提交于 2020-04-08 00:29:32
Vector: 写vector时候碰到这样一段代码: for(int j=0;j<v1.size();j++) { printf("%d %d",j,v1[j]); printf("%d",v1.size()); if(v1[j]==4) { v1.insert(v1.begin(),5); //在访问元素之前插入元素,那么元素的顺序整体就后面移动一位,那么一直访问到的都是4 j++; } getchar(); } Notice: 如果在遍历中需要插入删除元素,那么v1.size不能固定下来,否则会遗漏尾部的元素,在插入之后注意把迭代器或者索引向后移动一位,这样才会指向插入前那个元素的位置。在由迭代器加1指向下一个元素。 其实VECTOR本质上是一个可以容纳任何类型的动态数组。他用到了C++的动态数组,本质上是一个数组,存放在连续的区域,由此可以知道,中间插入或者删除会导致内存区域整块地移动,这样效率很低。所以Vector适合那些经常需要随机访问的类型,当让在末尾push_back,pop_back的速度是很快的,不会导致内存区域的整片移动。 // settest.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include<set> #include<algorithm> #include<iterator> using

C语言实现顺序表(增删)

亡梦爱人 提交于 2020-04-07 23:43:44
内容十分简单,不做过多的文字赘述。 #include<stdio.h> #define MAXSIZE 50 #define ElemType char typedef struct{ ElemType data[MAXSIZE]; int length; }sqlist; void Init(sqlist* l){ l->length = 0; } void CreateList(sqlist* l){ } /** * 顺序表的插入 * :平均时间复杂度O(n) * */ int ListInsert(sqlist* list,int i,ElemType e){ if(i<0||i>list->length+1){ printf("error:位置错误!"); return 0; }else if(list->length>=MAXSIZE){ printf("存储空间已满!"); return 0; }else{ for(int j=list->length;j>=i;j--){ list->data[j] = list->data[j-1]; } list->data[i] = e; list->length++; return 1; } return 0; } /** * 顺序表删除 * 平均时间复杂度:O(n) * */ int ListRemove(sqlist*