I'm a beginner to C and programming and I've got a problem with the counting words, digits, characters program examples in the beginning tutorial chapter of "the C programming language, second edition". Those program doesn't make sense to me. As the example says, I use the FOR loop to repeat the call of getchar() and count inputs. But the program is programmed to print the results of counting of inputs when the test fails, and the FOR loop is terminated. Since getchar() never encounters the EOF signal when I run/use the program in the commandline tool (typing input on the keyboard) the program stays in the FOR loop forever and never prints out the results. I understand that the program really counts number of inputs and stores it in a variable... but am I wrong that those examples are usless programs? Is there something i've missed with the meaning/use of the int EOF in those examples? What is the meaning with having getchar() != EOF as the test part in the loop (FOR) when that condition always becomes true (in those examples) since the value of EOF by defenition chouldn't be mixed up with things you type on the keyboard?
Code:
#include <stdio.h>
main()
{
int nc;
for (nc = 0; getchar() != EOF; ++nc)
;
printf("%d", nc);
}