End of File in stdin

In linux bash, if you press CTRL+D, it will generate EOF.

In Windows, the equivalent is CTRL+Z

So, no, if nothing written to the terminal, that does not generate EOF automatically. The scanning function is in wait state then. So, without having any other inputs, in wait state, if CTRL+D is pressed, the key press is translated [by the terminal driver] to EOF.Note

Usually, once you key in some value and press the ENTER key, the scannning function starts scanning. To feed an input for producing EOF, you need to press CTRL+D.

Related: Please reaed the wiki entry for EOF

Note: With thanks to Mr Drew for the clarification.


so if there is NOTHING written, End of File, (EOF) is not returned automatically?

No, it's not. It should be sent by the user.

So is it that only the user can invoke EOF in stdin by pressing Ctrl+Z?

Yes, you can set the EOF indicator for stdin with a special key combination you can input in the console, for linux console that is Ctrl+D and for windows it's Ctrl+Z.

If so then what are some of the uses of EOF in stdin? I guess it tells the program to continue reading until the user user invokes end of file? is this it?

The use of it depends on whether you instruct the user to input the EOF explicitly or not, for example, I think python console will tell you something like Press Ctrl+D or type quit() to exit.

And EOF is not necessarily -1 it's a macro and you should always use it to test for the EOF indicator. And more importantly EOF is not a character, it's a special value that indicates that the End Of File indicator is set.

Also, getchar() is equivalent to fgetc(stdin).

Tags:

C

Stdin

Console

Eof