The newline character is represented by \"\\n\"
in C code. Is there an equivalent for the end-of-file (EOF) character?
EOF
is not a character. It can't be: A (binary) file can contain any character. Assume you have a file with ever-increasing bytes, going 0 1 2 3 ... 255 and once again 0 1 ... 255, for a total of 512 bytes. Whichever one of those 256 possible bytes you deem EOF
, the file will be cut short.
That's why getchar()
et al. return an int
. The range of possible return values are those that a char
can have, plus a genuine int
value EOF
(defined in stdio.h
). That's also why converting the return value to a char
before checking for EOF
will not work.
Note that some protocols have "EOF" "characters." ASCII has "End of Text", "End of Transmission", "End of Transmission Block" and "End of Medium". Other answers have mentioned old OS'es. I myself input ^D on Linux and ^Z on Windows consoles to stop giving programs input. (But files read via pipes can have ^D and ^Z characters anywhere and only signal EOF when they run out of bytes.) C strings are terminated with the '\0'
character, but that also means they cannot contain the character '\0'
. That's why all C non-string data functions work using a char
array (to contain the data) and a size_t
(to know where the data ends).
Edit: The C99 standard §7.19.1.3 states:
The macros are [...]
EOF
which expands to an integer constant expression, with typeint
and a negative value, that is returned by several functions to indicate end-of-file, that is, no more input from a stream;
There is the constant EOF
of type int, found in stdio.h. There is no equivalent character literal specified by any standard.
I think it may vary from system to system but one way of checking would be to just use printf
#include <stdio.h>
int main(void)
{
printf("%d", EOF);
return 0;
}
I did this on Windows and -1
was printed to the console. Hope this helps.
The answer is NO, but...
You may confused because of the behavior of fgets()
From http://www.cplusplus.com/reference/cstdio/fgets/ :
Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first.