The code, derived from “The C Programming Language, Second Edition”:
#include <stdio.h>
main()
{
long nc;
nc = 0;
while(getchar() != EOF)
++nc;
printf("%s: %ld\n", "number of characters", nc);
}
Why doesn’t the “printf” statement execute after pressing the “enter” key (EOF)?
The enter key is not EOF. Depending on your system, it will send a carriage return (0x0D or
'\r'), a line feed (0x0A or'\n'), or both.On Linux (and probably Unix), EOF can be sent by Ctrl+D. On Windows it should be Ctrl+Z (but you might need to press enter afterwards).