The end of line (EOL) character is also read and counted by getchar()
, so it is included in your counts.
Ctrl-Z
Console input is often (and is on Windows), line buffered, meaning that your program will not see anything until the user presses Enter
.
Hence, you can type ^Z
anywhere, but until you press Enter
the text is not sent to your program’s input buffer to be read.
OS-issues
On Linux (and other *nixen) the EOL character is LF ('\n'
).
But on Windows it is a character sequence: CR LF ("\r\n"
).
In order to make the same code work on both *nix and Windows, when C opens the console file stream, it does so in text mode, which is otherwise identical to binary mode except that CR LF is reported to you as just LF. Hence, your experiments above report three characters ('m', 'y', and '\n') instead of four.