SCANF为什么将第一个值设置为0?
我正在使用scanf
获取输入。但是,当我运行此代码时:
#include<stdio.h>
int main(){
unsigned char in,b;
scanf("%u %u",&in,&b);
printf("%u %u\n",in,b);
return 0;
}
例如,当我输入12 3
时,输出为0 3
。
当我而不是scanf(“%u%u”,&amp; in,&amp; b);
write scanf(“%u”,&amp; in); scanf( “%u”,&amp; b);
。
感谢您伸出援手。
I am using scanf
to get an input. But when I run this code:
#include<stdio.h>
int main(){
unsigned char in,b;
scanf("%u %u",&in,&b);
printf("%u %u\n",in,b);
return 0;
}
when I input, for example, 12 3
, the output is 0 3
.
This also happends when I instead of scanf("%u %u",&in,&b);
write scanf("%u",&in);scanf("%u",&b);
.
Thanks for reaching out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SCANF的这种调用
调用了未定义的行为,因为该功能期望其第二和第三个参数是对
intemed int
的对象的指示,而实际上它们是对Unsigned char 。
相反,您必须使用转换说明器
u
从C标准(7.21.6.2 FSCANF函数)
This call of scanf
invokes undefined behavior because the function expects that its second and third arguments are pointers to objects of the type
unsigned int
while actually they are pointers to objects of the typeunsigned char
.Instead you have to use the length modifier
hh
with the conversion specifieru
From the C Standard (7.21.6.2 The fscanf function)
作为补充,如果
%hhu
不可用的转换说明符,您可以做:Just as an addition, in case of the
%hhu
conversion specifier being unavailable, you can do: