While studying getchar()
function in C ,I came across this EOF
being returned , I want to know how can its existence be noticed, where is it stored?
Can we type EOF
character explicitly?
While studying getchar()
function in C ,I came across this EOF
being returned , I want to know how can its existence be noticed, where is it stored?
Can we type EOF
character explicitly?
EOF
is short for End of File. It's not an actual character, but more like a signal that indicates the end of input stream.
Think about getchar()
, it's used to get a character from stdin
(the standard input). How could it tell when the stdin
stream has come to the end? There must be a special return value which is different from any valid characters. EOF
plays this role.
To generate EOF
for stdin
, type Ctrl + D on Unix systems, or Ctrl + Z on Windows.
EOF
is the named constant to apply for End Of File when reading from a stdio input stream. If you look at the getchar()
prototype, you'll first notice some strange thing is that it returns not a char
value, but an int
. Normally, EOF
translates in some negative integer value (historically -1
) meaning it's impossible to generate that character from the keyboard/input file.
Definitely, EOF
constant is not a character, is the int
value getchar(3) returns on end of file condition. This is also the reason of getchar(3) returning an int
instead of a char
value. It is also the reason always EOF
maps to a negative value.
getchar(3) returns one of 257 possible values, 0
up to 255
and EOF (which is normally -1
). Viewed as integer values, -1
is not the same as 255
. It's one of the oldest implemented functions in C (it's there since the first edition of "The C programming language" by K&R)
EOF is the abbr. for End-Of-File. It's the special character for indicating that you have reached the end of the file you're reading a file stream.
Normally, people check whether they have reached to the end of file by:
while(!feof(fileStream)) {
// read one line here or so
...
// do your stuff here.
...
}