I work through K & R 2nd edition, and I was puzzled by this seemingly simple example:
#include <stdio.h>
main(){
double c;
for(c = 0; ((getchar() != EOF) && (getchar() != '\n')); ++c)
;
printf("%.0f\n",c);
}
It does not work properly. I added in the section (getchar() != '\n')to end the program when I press the enter button, but that also does not help.
Here is an example of the output using gccthat comes with the Mac OSX 10.6 dev tools.
pool-000:Desktop user$ ./a.out
a
0
pool-000:Desktop user$ ./a.out
asdf
2
pool-000:Desktop user$ ./a.out
asfasf
3
So, something is clearly wrong. I am on page 18 if this helps. This is not homework, it is for fun!
Thanks:)
Isaac source
share