This example is from the K&R book
#include<stdio.h>
main()
{
long nc;
nc = 0;
while(getchar() != EOF)
++nc;
printf("%ld\n", nc);
}

Could you explain me why it works that way. Thanks.
^Z^Z doesn’t work either (unless it’s in the beginning of a line)

Traditional UNIX interpretation of tty
EOFcharacter is to make blockingreadreturn after reading whatever is buffered inside a cooked tty line buffer. In the start of a new line, it meansreadreturning 0 (reading zero bytes), and incidentally, 0-sizedreadis how the end of file condition on ordinary files is detected.That’s why the first
EOFin the middle of a line just forces the beginning of the line to beread, not making C runtime library detect an end of file. TwoEOFcharacters in a row produce 0-sized read, because the second one forces an empty buffer to bereadby an application.I assume that your C runtime library imitates the semantics described above (there is no special handling of
^Zat the level ofkernel32calls, let alone system calls, on Windows). That’s why it would probably detect EOF after^Z^Zeven in the middle of an input line.