#include <stdio.h>
// copy input to output
// my version
int main()
{
int c;
printf("\n\nUse CONTROL + D to terminate this program\n\n");
while ((c = getchar()) != EOF) {
putchar(c);
}
if ((c = getchar()) == EOF) {
printf("\n\nProgram TERMINATED\n\n");
}
return 0;
}
獲取輸入當我進入控制 + d,該if
語句運行的機構。這就是我想要的,但是當我更徹底地分析代碼時,不應該再次詢問我的輸入,因爲if
的條件是(c = getchar()) == EOF
?在if條件
爲什麼'int'而不是'char'? – alex
@alex:因爲'getchar'返回一個'int'(實際上是一個'unsigned char'轉換爲'int'或者'EOF' - 根據定義它是一個負數)。 – jweyrich