scanf("%d%#d%d",&a,&b,&c);
如果给定值1 2 3,则输出为1 0 0
P.S-我知道它与printf()语句一起使用,但是在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 both0
,a single0
is printed). Forx
(orX
) conversion,a nonzero result has0x
(or0X
) prefixed to it. Fora
,A
,e
,E
,f
,F
,g
,andG
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.) Forg
andG
conversions,trailing zeros are not removed from the
result. For other conversions,the behavior is undefined.