0

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?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Amisha Bansal
  • 11
  • 1
  • 4

3 Answers3

3

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.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
1

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)

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31
0

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.
    ...
}
Envil
  • 2,687
  • 1
  • 30
  • 42
  • `EOF` is not a character. It's a signal. Also most uses of `feof()` like in your answer are probably wrong. – pmg Jul 02 '15 at 08:15