我读过周围的内容,每个人都说在扫描单个字符时使用" %c",因为输入流的开头可能有空格。
好吧,公平。但这仅适用于所述输入流中的第一个字符吗?如果我现在扫描由许多连续字符组成的字符串中的单个字符怎么办?(例如:abcdef)
" %c"现在用作格式说明符不会把事情弄乱吗?
是否scanf("%d%d");像scanf("%d %d");?
或者
是否scanf("%d %d");像scanf("%d%d);?
或者
他们是不一样的?
我知道空白会消耗缓冲区中的任何输入空白。
当我输入[32 44]他们如何区分 32 是第一%d和 44 是第二%d,如果是真的?
所以,这是我的 C 代码。当我为了学习格式说明符而摆弄我的代码时,尤其是%c:
#include <stdio.h>\n\nvoid main()\n{\n double a = 0.21;\n printf("%c", a);\n}\nRun Code Online (Sandbox Code Playgroud)\n我碰巧遇到这样的情况:即使我将浮点值作为printf()格式说明符的函数参数传递%c,编译器(我的编译器是gcc)也会以某种方式将该值转换为与 ASCII 字符相对应的0.21十进制值。223\xc3\x9f
对于a = 0.22输出为:)其 ASCII 表中的十进制值为29
我在VS code和CLion IDE上运行了代码,但结果是相同的。现在这让我摸不着头脑好几天了,我无法弄清楚。
\n我想知道值0.21和0.22是如何转换成十进制的223,29或者它们如何分别对应于 ASCII 223 和 29 ie\xc3\x9f和)。
由于值 0.21、0.22 不对应于任何 ASCII,因此我希望程序不会打印任何内容。
\n但根据输出,我认为这可能与二进制文件有关。
\n由于二进制中的 0.21 是 0.00110101110000101000111101011100...
\n& 223 是 11011111 …
我有类似的东西
char string[]="My name is %s";
printf("%s",string,"Tom");
Run Code Online (Sandbox Code Playgroud)
我期待输出,My name is Tom但我得到了My name is %s
我尝试逃避%角色,但它有效.
这里出了什么问题?我怎么会有%s一个%s?
当我%u在sprintf()应用程序中使用时崩溃,它的工作正常%d
看代码:
#include <stdio.h>
#include <string.h>
main()
{
unsigned char dAddr[4];
unsigned char sMask[4];
unsigned char nHop[4];
memset(dAddr,0,sizeof(dAddr));
memset(sMask,0,sizeof(sMask));
memset(nHop,0,sizeof(nHop));
unsigned int u4IpDAddr = 0x01020304;
unsigned int u4IpSNetMask = 0xffff01ff;
unsigned int u4NHopGt = 0x01020304;
char *dip = (char *)&u4IpDAddr;
char *smk = (char *)&u4IpSNetMask;
char *nhp = (char *)&u4NHopGt;
sprintf(dAddr, "%u.%u.%u.%u", dip[3], dip[2], dip[1], dip[0]); //if I used %d.%d.%d.%d its working fine
sprintf(sMask, "%u.%u.%u.%u", smk[3], smk[2], smk[1], smk[0]); //if I used %d.%d.%d.%d its working …Run Code Online (Sandbox Code Playgroud) #include<stdio.h>
void main()
{
int n = 2;
printf("%c",&n);
}
Run Code Online (Sandbox Code Playgroud)
输出:L
在使用%d它当然给出了变量的地址,n但为什么在使用时输出L %c?
#include<stdio.h>
#define PRINT(A,B) printf("Value of expression %s is %*",#A,#B,(A))
int main(void){
PRINT(1+3+1,%d);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何编写一个宏它得到两个参数:expression和format specifier然后打印在给定的格式它的表达和价值?
我从这里学习C语言中与时间相关的函数.他们使用以下示例演示了strftime()函数:
#include <stdio.h>
#include <time.h>
#define LEN 150
int main ()
{
char buf[LEN];
time_t curtime;
struct tm *loc_time;
//Getting current time of system
curtime = time (NULL);
// Converting current time to local time
loc_time = localtime (&curtime);
// Displaying date and time in standard format
printf("%s", asctime (loc_time));
strftime (buf, LEN, "Today is %A, %b %d.\n", loc_time);
fputs (buf, stdout);
strftime (buf, LEN, "Time is %I:%M %p.\n", loc_time);
fputs (buf, stdout);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我对printf()中的%m说明符进行了调整.它说%m转换说明符不是C,而是printf的GNU扩展.'%m'转换在errno中打印对应于错误代码的字符串.
我知道格式说明符%是C99中的新内容.它以十六进制形式打印浮点数. …
这里有一个奇怪的代码:
const double a[] = {0,1,2,3,4};
int main()
{
double *p = a;
printf("%f\n",p[2]); //2.000000
printf("%f\n",p); //2.000000
}
Run Code Online (Sandbox Code Playgroud)
它返回2.000000,为什么?
#include<stdio.h>
#include<stdlib.h>
struct Graph
{
int v;
};
int main()
{
struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
graph -> v = 1;
printf("%u", graph);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是我收到有关该行格式的警告:
printf("%u", graph);
Run Code Online (Sandbox Code Playgroud)
警告是:
/home/praveen/Dropbox/algo/c_codes/r_2e/main.c | 14 |警告:格式'%u'期望参数类型为'unsigned int',但是参数2的类型类型为'struct Graph *'[-Wformat =] |
我应该为类型使用什么格式说明符struct Graph *?
这是我的代码,我不知道为什么会发生这种情况:
#include <stdio.h>
void foo(float *);
int main()
{
int i = 10, *p = &i;
foo(&i);
}
void foo(float *p)
{
printf("%f\n", *p);
}
Run Code Online (Sandbox Code Playgroud)
输出:
0
Run Code Online (Sandbox Code Playgroud) 我偶然发现了我很好理解的这种行为.
我错误地在程序结束时编写了以下内容,以便在以下数组中打印元素char:
printf("output: %s",outputText[]);
Run Code Online (Sandbox Code Playgroud)
当我应该(并最终)迭代数组并打印每个元素时:
for(int i = 0; i < textLength; i++){
printf("%c",outputText[i]);
}
Run Code Online (Sandbox Code Playgroud)
促使我实施后者的是我得到的输出.尽管初始化数组以限制字符outputText[textLength],确保数组中没有意外的元素,但是在实现前一代码时我的输出总是会被其他怪异的元素所占据,如下所示:
我只是连续三次运行相同的程序,并在我的数组末尾附加了三个随机字符.
(编辑替换outputText[]- > outputText在第一个示例中.)
假设我使用了printf没有传递足够的参数来匹配格式说明符:
#include <stdio.h>
int main(void) {
printf("missing argument: %s\n");
}
Run Code Online (Sandbox Code Playgroud)
对结果有什么保证吗?
在我的机器上,根本没有打印.
总是这样,或者是否有可能用解析的说明符打印字符串?
例如:
missing argument: %s
要么:
missing argument: