dasha tokarev
dasha tokarev

Reputation: 21

EOF not recognized

I have a program that is supposed to get input from a user until it receives EOF.

\n or white spaces are considered as legal chars, but the console recognizes neither ^Z nor ^D as EOF and the program continues to run until stopped manually.

Tried both:

while (currChar != EOF)
{
    scanf("%c", &currChar);
}

and:

scanf("%c", &currChar);
if (currChar == EOF)
    break;

Upvotes: 1

Views: 765

Answers (2)

Mister levelS
Mister levelS

Reputation: 1

Well, EOF is not a character. It's an integer constant with the value of -1. With that said if you enter with the value of EOF, your char variable would read only the - (minus) from -1 value. That's why the loop won't stop, they'll never be equal.

Upvotes: 0

Barmar
Barmar

Reputation: 780984

scanf() doesn't set the variable to EOF when it gets to the end of the input, it returns EOF. So you have to test the value of the function.

while (scanf("%c", &currChar) != EOF) {
    ...
}

Upvotes: 5

Related Questions