mdjukan
mdjukan

Reputation: 41

Can't read anything after sending EOF?

#include <stdio.h>
int main()
{
    char c = getchar(); //EOF (ctrl + d )
    while( ( c = getchar() ) != '?' )
    {
        printf( "%d\n", c == EOF );//infinite loop printing 1
    }
}

What happens here?

It is as if EOF completely blocks reading anything after it?

Upvotes: 3

Views: 89

Answers (1)

kaylum
kaylum

Reputation: 14046

You need to call clearerr on stdin to clear the EOF. Also, note that getchar returns an int and not a char because EOF does not fit in a char.

Upvotes: 7

Related Questions