無法重現你的錯誤。我加了一些調試語句,
#include <stdio.h>
main() {
int nc = 0, ch;
while ((ch = getchar()) != EOF) {
printf("%d\n", ch);
++nc;
}
printf("nc - %1d\n", nc);
}
然後用gcc
試過在Windows上:
E:\temp>gcc eof.c
E:\temp>a
^Z
nc - 0
E:\temp>a
foo bar
102
111
111
32
98
97
114
10
^Z
nc - 8
然後與Visual Studio 2008:
E:\temp>cl eof.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
eof.c
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:eof.exe
eof.obj
E:\temp>eof
^Z
nc - 0
E:\temp>eof
foo bar
102
111
111
32
98
97
114
10
^Z
nc - 8
我假設你的意思是「它打印2 「,而不是」nc乘以2「。我猜Ctrl + Z正在生成2個按鍵。 –
因爲'enter'是一個按鍵。 –
哦,我現在看到了,這就是爲什麼每個按鍵都註冊兩次而不是一次。 – Emryss