Section 1.5.2 Character Counting#
Program#
#include <stdio.h>
/* count characters in input; 1st version */
main()
{
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n", nc);
}
Explanation#
In this program we are going to count the number of characters present in the input. The program does the counting by setting nc to 0 in the beginning. As the program enters while loop condition (getchar() != EOF). When nc hits end of the document it prints the number of characters in the file.
See also
This document was updated on 20 May of 22