Straight from K & R, I have
#include <stdio.h>
main() {
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n", nc);
}
I am on Mac OS X, so I compile it, run it, type “12345”, press enter for a new line (this is the sixth char I think), and then press ctrl-D to send EOF.
Thing prints "6D". Why is there D? How to write a program to simply count 5 characters in "12345" and not on a new line? Should I just subtract it at the end? How can I make him stop printing D?
source
share