#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
請閱讀您要使用的所有庫函數的手冊。 –