Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

End of File (EOF) in C

Tags:

c

eof

I am currently reading the book C Programming Language by Ritchie & Kernighan. And I am pretty confused about the usage of EOF in the getchar() function.

First, I want to know why the value of EOF is -1 and why the value of getchar()!=EOF is 0. Pardon me for my question but I really don't understand. I really tried but I can't.

Then I tried to run the example on the book that can count the number of characters using the code below but it seems that I never get out of the loop even if I press enter so I am wondering when would I reach the EOF?

main(){    long nc;    nc = 0;    while (getchar() != EOF)        ++nc;    printf("%ld\n", nc); } 

Then, I read the same problem at Problem with EOF in C. Most people advised that instead of using EOF, use the terminator \n or the null terminator '\0' which makes a lot of sense.

Does it mean that the example on the book serves another purpose?

like image 854
newbie Avatar asked Dec 05 '10 12:12

newbie


People also ask

How do you end EOF?

If you're typing at the terminal and you want to provoke an end-of-file, use CTRL-D (unix-style systems) or CTRL-Z (Windows). Then after all the input has been read, getchar() will return EOF , and hence getchar() != EOF will be false, and the loop will terminate.

What is the value of EOF end of file?

EOF instead is a negative integer constant that indicates the end of a stream; often it's -1, but the standard doesn't say anything about its actual value. C & C++ differ in the type of NULL and '\0' : in C++ '\0' is a char , while in C it's an int ; this because in C all character literals are considered int s.

What is EOF mean?

end-of-file: a code, marker, or signal used to indicate the end of a file of data.


1 Answers

EOF indicates "end of file". A newline (which is what happens when you press enter) isn't the end of a file, it's the end of a line, so a newline doesn't terminate this loop.

The code isn't wrong[*], it just doesn't do what you seem to expect. It reads to the end of the input, but you seem to want to read only to the end of a line.

The value of EOF is -1 because it has to be different from any return value from getchar that is an actual character. So getchar returns any character value as an unsigned char, converted to int, which will therefore be non-negative.

If you're typing at the terminal and you want to provoke an end-of-file, use CTRL-D (unix-style systems) or CTRL-Z (Windows). Then after all the input has been read, getchar() will return EOF, and hence getchar() != EOF will be false, and the loop will terminate.

[*] well, it has undefined behavior if the input is more than LONG_MAX characters due to integer overflow, but we can probably forgive that in a simple example.

like image 60
Steve Jessop Avatar answered Sep 22 '22 21:09

Steve Jessop