首页 > 解决方案 > 使用“getchar()”和“EOF”期间的无意义输出流

问题描述

我一直在尝试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;
}

输入: -

一个

预期输出:-

一个//Due to putchar()

-1//Value of EOF

//Now the cursor should come in next line and wait for next character.

实时输出:-

一个

-1

-1

//Cursor waiting for next character.

我无法理解输出显示-1两次的原因。

标签: ceofgetcharputchar

解决方案


您的代码注释说

//Now the cursor should come in next line and wait for next character.

但是第二个循环不会等待。它读取已经输入的换行符,这由输出中的额外空白行显示。


推荐阅读