【发布时间】:2015-03-16 01:54:45
【问题描述】:
我看到这个程序读取整个字符串:
#include <stdio.h>
int main(int argc, char *argv[]){
int something;
printf("Enter something \n");
while (scanf("%c", &something)==1) {
printf("%c", something);
}
return 0;
}
当我输入hello world 时,它会输出hello world。
谁能解释一下为什么不输出:
h
e
l
l
...
因为我认为循环一次遍历一个字母,所以我很困惑为什么它没有像那样输出。现在,我尝试编写一个使用 getchar 来做同样事情的程序:
#include <stdio.h>
int main()
{
char c;
printf("Enter character: ");
c = getchar();
printf("Character entered: ");
putchar(c);
return(0);
}
当我输入hello world 时,这个程序只会输出h。如何使用 getchar 做与第一个程序相同的事情?还有getchar和scanf有什么区别?
【问题讨论】: