I have the following code from The C Programming Language, that return the number of characters typed
#include <stdio.h>
/* count characters in input; 1st version */
main() {
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n", nc);
}
However, when I execute my program (charcounter.exe) in the command prompt in Windows, and enter a sequence of characters, the program prints one more. Just like in the following example
C:\...\c-folder>charcounter
abc
^Z
4
Why is this happening? I suppose maybe is because the ENTER I press, but when I try to type the entire sequence, then Ctrl + Z, and then ENTER, the program doesn't read the EOF character and keeps waiting for another.
C:\Users\juani\Desktop\c-folder>charcounter
abc^Z
^Z
4