【发布时间】:2020-07-27 10:42:31
【问题描述】:
我用 C 语言编写代码,但是当我这样写时
int main()
{
char one,two,three;
int num=0;
scanf("%d",&num);
scanf("%c %c %c",&one,&two,&three);
printf("%c %c %c",one,two,three);
}
当我输入字符 a b c 时显示我
a b
并且不显示c
当我输入num 时会发生这种情况。如果我将num 的变量设置为3 它会显示我
a b c
喜欢我的意见
如何解决?
【问题讨论】:
-
在处理键盘输入/输出时,最好先使用 fgets(),然后使用 sscanf()
-
%d 在非数字上停止并将其留在缓冲区中。使用 fgets 读取整行,使用 sscanf 尝试读取 num,但检查返回值。
标签: c validation input