This example is from the K&R book
#include
main()
{
long nc;
nc = 0;
while(getchar() != EOF)
++nc;
printf(\"%ld
EOF indicates "end of file". A newline (which is what happens when you press enter) isn't the end of a file, it's the end of a line, so a newline doesn't terminate this loop.
Depending on the operating system, EOF
character will only work if it's the first character on a line, i.e. the first character after an Enter
. Since console input is often line-oriented, the system may also not recognize the EOF
character until after you've followed it up with an Enter
.