有人能解釋我爲什麼我看到的printf的雙輸入()函數while循環:奇怪的printf()在while循環中的行爲
#include <ctype.h>
#include <stdio.h>
int main(){
int x = 0;
while (x != 'q'){
printf("\nEnter a letter:");
x=getchar();
printf("%c\n", x);
if(isalpha(x))
printf("You entered a letter of the alphabet\n");
if(isdigit(x))
printf("You entered the digit %c\n", x);
}
return 0;
}
代碼的Debian的擠壓輸出( gcc版本4.4.5(Debian的4.4.5-8))是:
Enter a letter:1
1
You entered the digit 1
Enter a letter: // why is the first one appearing ???
Enter a letter:2
2
You entered the digit 2
「回車」鍵產生另一個字符。讓你的循環無條件地打印char值,你會看到。你爲什麼認爲「printf表現得很奇怪」? –