Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » character counting program in C(get one too many counts in an example program)
character counting program in C [message #1724636] Thu, 25 February 2016 04:27 Go to next message
Daniel Daigle is currently offline Daniel DaigleFriend
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 Go to previous message
Tauno Voipio is currently offline Tauno VoipioFriend
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
Previous Topic:Python interpreter
Next Topic:Eclipse 3.8 (Oracle Weblogic Portal eclipse IDE)
Goto Forum:
  


Current Time: Tue Apr 23 16:37:06 GMT 2024

Powered by FUDForum. Page generated in 0.08083 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top