我一直在尝试使用 getchar()
、putchar()
并尝试使用 EOF
。下面是我一直在尝试的代码片段。
#include <stdio.h>
int main(void)
{
int c;
c = getchar();
while(c != EOF)
{
putchar(c);
printf("\n");
printf("%d\n", EOF);
c = getchar();
}
return 0;
}
Input: -
a
Expected output: -
a
//Due to putchar()
-1
//Value of EOF
//Now the cursor should come in next line and wait for next character.
Output getting in real time: -
a
-1
-1
//Cursor waiting for next character.
我无法理解输出显示 -1
两次的原因。
最佳答案
你的代码注释说
//Now the cursor should come in next line and wait for next character.
但是第二个循环不会等待。它读取已输入的换行符,这由输出中的额外空行显示。
关于c - 使用 "getchar()"和 "EOF"期间出现无意义的输出流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59159591/