scanf()函数中%符号后的#符号是什么意思?

以下代码在C中的含义是什么
scanf("%d%#d%d",&a,&b,&c);

如果给定值1 2 3,则输出为1 0 0

P.S-我知道它与printf()语句一起使用,但是在scanf()语句中它给出了随机行为.

解决方法

TL; DR; – 在scanf()函数的格式字符串中的%符号后面的#是错误的代码.

说明:

#here是一个标志字符,在fprintf()和family中允许,而不是在fscanf()和family中.

对于您的代码,#after%的存在被视为无效的转换说明符.根据7.21.6.2,

If a conversion specification is invalid,the behavior is undefined

所以,你的代码产生undefined behaviour.

提示:您可以检查scanf()的返回值,以检查已成功“扫描”了多少元素.

但是,在printf()中使用#with%d的FWIW也是undefined behaviour.

仅供参考:根据C11标准文件,章节§7.21.6.1,标志字符部分,(强调我的)

#

The result is converted to an ‘‘alternative form’’. For o conversion,it increases the precision,if and only if necessary,to force the first digit of the result to be a zero (if the value and precision are both 0,a single 0 is printed). For x (or X) conversion,a nonzero result has 0x (or 0X) prefixed to it. For a,A,e,E,f,F,g,and G conversions,the result of converting a floating-point number always contains a decimal-point character,even if no digits follow it. (Normally,a decimal-point character appears in the result of these conversions only if a digit follows it.) For g and G conversions,trailing zeros are not removed from the
result. For other conversions,the behavior is undefined.

相关文章

一.C语言中的static关键字 在C语言中,static可以用来修饰局...
浅谈C/C++中的指针和数组(二) 前面已经讨论了指针...
浅谈C/C++中的指针和数组(一)指针是C/C++...
从两个例子分析C语言的声明 在读《C专家编程》一书的第三章时...
C语言文件操作解析(一)在讨论C语言文件操作之前,先了解一下...
C语言文件操作解析(三) 在前面已经讨论了文件打开操作,下面...