From The C Programming Language:
int c;
while ((c = getchar()) != EOF)
putchar(c);
"... The solution is that getchar
returns a distinctive value when there is no more input, a value that cannot be confused with any real character. This value is called EOF
, for "end of file." We must declare c
to be a type big enough to hold any value that getchar
returns. We can't use char
since c
must be big enough to hold EOF
in addition to any possible char
."
I checked in stdio.h
and printed the value of EOF on my system, and it's set to -1
. On my system, chars
are signed, although I understand that this is system dependent. So, EOF
can fit in a char
for my system. I rewrote the small routine above by defining c
to be a char
and the program works as intended. There's also a character in the ASCII character table here that appears to have a blank character corresponding to 255 which appears to act like EOF
.
So, why does it appear that ASCII has a character (255) designated for EOF? This seems to contradict what is said in the The C Programming Language book.