Ctrl+Z is an ASCII control character like Ctrl+A, Ctrl+B, Ctrl+@ and the others. It is not EOF
. EOF
is distinct from any character: otherwise, if getchar()
returned that value, you couldn't know whether it had reached the end of the file or it had read that character. Ctrl+Z has the ASCII value 26. You're seeing what you typed.
On your operating system (Windows, presumably), Ctrl+Z at the beginning of a line is an indication to the terminal that you've finished typing. (More precisely, I think the code is in the library functions that read from the terminal, and not from the terminal itself. But I may be wrong on the details as I'm not a Windows expert.) It only works at the beginning of a line because that's how the library code that reads from the terminal works. When you type Ctrl+Z at the beginning of a line, getchar()
sees an end-of-file condition and returns EOF
.
When you type Ctrl+Z not at the beginning of a line, it's treated as an ordinary character. Windows prints it out as a “strange glyph” that it uses to indicate an unprintable character.
The official meaning of most ASCII characters was forgotten a long time ago. There is an ASCII control character that used to mean “end of file”, or more precisely “end of transmission”, which is Ctrl+D, and it survives in the Unix operating system family to mean “end of input” when entered at the beginning of a line on a terminal, much like Ctrl+Z on Windows. The association of Ctrl+Z with end-of-file on Windows comes from a different historical line of operating systems, mostly from DEC, that Microsoft's DOS and Windows took inspiration from.
Not exactly the same question as yours, but I recommend this answer which should help clear your confusion about EOF
.