C语言:统计输入的字符数

在书上看到的程序
#include "stdio.h"
main()
{
long nc;
nc=0;
while(getchar()!=EOF)
++nc;
printf("%ld\n",nc);
}
编译时没有错误,运行时输入一串字符,然后按回车,什么也不显示,怎么回事?

废话,按回车也getchar()!=EOF,然后你就一直输吧,没个头
你得把stdin给direct到其他文件里,比如你写个a.txt,然后你compile之后打个
[command name] < a.txt
就有了= =
你要想回车之后就有东西的话就把EOF改成new line character
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-12-29
#include "stdio.h"
main()
{
long nc;
nc=0;
while(getchar()!='\n')
++nc;
printf("%ld\n",nc);
}
相似回答
大家正在搜