Search code examples
cgetchar

Why getchar is reading one more value?


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

Solution

  • Summing it up a little bit Ctrl-Z only behaves as EOF when it's at the beginning of a line, if it's elsewhere all the characters in that line from ^Z onwards will be ignored, that includes the \n, so for instance, for an input of abcdctrl-zasdEnter, only 4 characters are counted in that line.

    The rest is what you said, \n is also a character, so it gets counted, you can always exclude it from being counted with a condition if that is an option:

    int main()
    {
        long nc;
        nc = 0;
        int c;
        while ((c = getchar()) != EOF)
            if (c != '\n')
                ++nc;
        printf("%ld\n", nc);
    }