Hey guys. Just started learning to code, and loving it so far. However, I have hit a snag with the often recommended C Programming Language by Kernighan and Ritchie, so I thought I would ask here.
In section 1.5.3, he gives a program for counting characters,
Code:
#include <stdio.h>
main()
{
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n", nc);
}
which is supposed to count the characters and then display it, but the program never displays the number of characters when I compile it in codeblock 10.05. Moreover, I can get it to count it out if I put the printf inside the loop, but I always gets one more number than the actual number of characters I type in. I have the same problem with
Code:
#include <stdio.h>
main()
{
double nc;
for(nc = 0; getchar() != EOF; ++nc)
;
printf("%.0f\n", nc);
}
so I was just hoping someone could help with this. Thanks a lot.