character counting program in C [message #1724636] |
Thu, 25 February 2016 04:27 |
Daniel Daigle Messages: 2 Registered: February 2016 |
Junior Member |
|
|
I took the character counting program from the book, The C Programming Language 2nd ed, and it works fine on online compilers, but for some reason i get one extra count in eclipse. here is the program
#include <stdio.h>
int main(void)
long nc;
nc=0;
while (getchar() != EOF)
++nc;
printf ("%1d\n", nc);
return 0;
}
for example, if i input jkl, then hit enter, then terminate, I get the value of 4.
i also tried it in the for loop with the same result.
Any ideas whats going on here?
is it counting enter as a character?
if i dont hit enter it will print 0
is there a way to skip hitting enter and the terminate button?
also
if i printf("%d\n",EOF); it prints -1
|
|
|
Re: character counting program in C [message #1724677 is a reply to message #1724636] |
Thu, 25 February 2016 12:03 |
Tauno Voipio Messages: 742 Registered: August 2014 |
Senior Member |
|
|
This is not actually an Eclipse question, but a general C programming question instead. A better forum could be e.g. stackoverflow.
Please read your book once again: The newline (from the Enter key) *is* a chracter. The keyboard driver needs to know when the input line ends, so you do not get a bunch of characters without a newline ('\n').
--
Tauno Voipio
|
|
|
Powered by
FUDForum. Page generated in 0.08083 seconds