#include <stdio.h>
int main(void) {
int x = 0;
while (1){
x = scanf("%d",&x);
printf("%d",x);
}
return 0;
}
#include <stdio.h>
int main(void) {
int x = 0;
while (1){
x = scanf("%d",&x);
printf("%d",x);
}
return 0;
}
|
wevstyOct 26, 2018
1
scanf 的返回值并不是输入的内容。
|
|
messyideaOct 26, 2018
2
输入字母不应该是 %c 么
|
|
GinrayOct 26, 2018
3
一楼正解
|
|
yuiknsOct 26, 2018
4
https://linux.die.net/man/3/scanf
These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure. |
|
xxgirl2Oct 26, 2018
5
|
|
zuoyouTUOct 26, 2018
6
稍补充下,等号表达式最后执行
|
|
a516585610Oct 26, 2018 via Android
7
%d 输入字母会失败,scanf 返回 0 输出当然也是 0
|
|
webdiskOct 26, 2018 via Android
8
while (1) 后面是不是少个空格
|
|
9
针对一直输出的问题,或许试试清除缓冲区?
|
|
10
@wevsty 这个我知道啊,输入字母 scanf 返回 0,打印 0 能理解,但是为什么一直在打印 0 啊?求教
|
|
xiaopcOct 26, 2018 via Android
11
@codechaser 因为你 while (1) 了
|
|
12
@webdisk 这跟空格有啥关系啊😓
|
|
13
@xiaopc 但是输入数字就能多次输入
|
|
14
@aopod 谢谢老哥!我在`x=scanf("%d",&x)`后面加了句`setbuf(stdin,NULL);`就没有一直输出了!感谢!能讲解下原因吗
|
|
15
@codechaser 对对,是缓存区的问题。
scanf 没从 stdin 读到对应数据不会清除缓冲区。 |
|
16
@xiaopc 是的,scanf 在这里会跳过非数字,把字母一直留在缓冲区,所以每次读入的都是之前输入的字母,也就一直打印 0 了
|
|
jimchen9999Oct 26, 2018
17
the issue lies with the buffer in C
|
|
webdiskOct 26, 2018
18
@codechaser 没关系,但是不符合编码规则我就难受
|