Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Count Characters on STDIN

  • If in the while loop there is only one statement, we don't need to put it in curly braces. I don't really like this, but this is how C is.
#include<stdio.h>


int main() {
    long count = 0;

    while (getchar() != EOF)
        count++;

    printf("Number of characters: %ld\n", count);

    return 0;
}