Hey, so I've wrote two programs in C to count characters and print the value but neither seem to work. One uses the while loop and the other uses for, no errors are reported while compiling, but neither print the total count. Or anything for that matter.
Here's the code using while:
#include <stdio.h>
/* count characters and input using while */
main()
{
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n", nc);
}
And here's the code using for:
#include <stdio.h>
/* count characters and input using for */
main()
{
long nc;
for (nc = 0; getchar() != EOF; ++nc)
;
printf("%ld\n", nc);
}
Both compile ok and run. When I type input and hit enter, a blank newline prints. When I simply hit enter without inputting anything, again a blank newline prints (and I'd think it would at least print zero). I'm at a loss as to why, everything seems ok... so if you could help me out here I'd really appreciate it.