以非常简单的程序为例。
#include <stdio.h>
void main()
{
int c; // has to be int so it's large enough to hold EOF
printf("Please press EOF (C^D in Linux)\n");
while ((c = getchar()) != EOF)
printf("That's not EOF! Try again.\n"); // why is this printed twice?
printf("EOF is ");
putchar(c);
printf(".\n");
}
问题也很简单:为什么第8行执行两次?它不应该这样工作吗:
c
。c
是否不是EOF
。相反,步骤3是:
令人困惑的问题。有人会向我解释为什么会这样以及如何解决吗?