getchar()的问题
//*题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。//1.程序分析:利用while语句,条件为输入的字符不为'\n'.
程序代码:#include "stdio.h"
int main(void)
{
int letters = 0, spaces = 0, digits = 0, others = 0;
char c ;
printf("please input some characters:\n");
while ((c=getchar())!= '\n')
{
if (c >= 'a'&&c <= 'z' || c >= 'A'&&c <= 'Z')
letters++;
else if (c >= '0'&&c <= '9')
digits++;
else if (c == ' ')
spaces++;
else
others++;
}
printf("letters=%d,spaces=%d,digits=%d,others=%d",letters,spaces,digits,others);
return 0;
}我在这儿要问的是c = getchar();这个语句为什么不能从while语句中抽出来放在while语句前面,就是写成:
c =getchar();
while(c != '\n')
{
}
请问为什么不能这样写;








那么循环体只会执行一次,也就是只会判断你输入的第一个字符。。。