#include <stdio.h>
int main(void)
{
char ch;
int count;
while((ch=getchar())!=EOF){
if(ch==' '){
count++;
}
}
printf("total words is %d",count);
return 0;
}
我的C代码是在这里,我很困惑,我把CTRL + Z直接在Windows中,转出数为2?为什么走循环了两个地方来空间?反视的getchar返回值给出错误oputput
分配取之于'的getchar()返回'到具有可变类型'char'是不好的,因为它会截断数据并且它在典型环境中区分不出一个charcter和'EOF'。使用'int'很好。 – MikeCAT
相关的,如果不是一个笨蛋:http://stackoverflow.com/q/35356322/694576 – alk
请阅读您要使用的所有库函数的手册。 –