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

前端之家收集整理的这篇文章主要介绍了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.

原文链接:https://www.f2er.com/c/117380.html

猜你在找的C&C++相关文章