+ 6

Problem with c program in ā€˜The C bookā€™

Ok so, in the second edition of ā€œThe C programming Languageā€ by Brain and Dennis . There is a chapter 1 page 18 The second code #include <studio.h> main() { double nc; for(nc=0; getchar() != EOF; ++nc); printf("%.0f\n", nc); } Itā€™s suppose to be a character counting program, it works if the last character you input is a new line, then it counts everything.. but I noticed if I donā€™t add a new line and before sending EOF it returns 0 .. why is that?

17th Dec 2022, 2:32 PM
Wasiu Abass
Wasiu Abass - avatar
10 Answers
+ 8
The issue you are experiencing is due to the way the getchar function works. getchar reads a character from the standard input (e.g. keyboard) and returns it as an int value. If it encounters the end of the input stream (EOF), it returns a special constant called EOF. In the code you posted, the getchar function is used within the condition of a for loop. The loop will continue as long as the value returned by getchar is not equal to EOF. However, the getchar function will not return EOF until it has reached the end of the input stream, which in this case means that it has read all the characters that were typed in. If you press Ctrl+D (on Unix-like systems) or Ctrl+Z (on Windows systems) to signal the end of the input stream before typing a newline character, the getchar function will not have read any characters, so the loop will not execute even once, and the value of nc will remain 0.
17th Dec 2022, 6:55 PM
Sadaam Linux
Sadaam Linux - avatar
+ 6
One way to fix this issue is to modify the loop condition to check for a newline character or EOF, like this: for(nc=0; getchar() != '\n' && getchar() != EOF; ++nc); This way, the loop will continue as long as the character being read is not a newline or EOF.
17th Dec 2022, 6:55 PM
Sadaam Linux
Sadaam Linux - avatar
+ 3
I learnt C a long time ago...The newline character was '\0', also called as a SENTINEL, and any fopen, fread and fclose, Used this. Try this out...should work !
18th Dec 2022, 3:57 PM
Sanjay Kamath
Sanjay Kamath - avatar
+ 2
Programmer Programming with c++ by D Ravichandran
19th Dec 2022, 3:19 AM
filibuster
filibuster - avatar
+ 1
Guys! Who can recommend book or courses by C and C++ ? Thanks
19th Dec 2022, 3:05 AM
Programmer
0
what you mean by "add the new client"??
17th Dec 2022, 4:19 PM
arturop200
arturop200 - avatar
0
Mistake let me re-edit that
17th Dec 2022, 4:29 PM
Wasiu Abass
Wasiu Abass - avatar
0
whats the exact input that makes it print zero so i can replicate it. cus for me its working fine(linux). Just the EOF is annoying to enter in terminal, when managing files no problem at all. Btw may i suggest reading other books? I know that the one by K&R is mythical but its been 30yrs+. Ive read others better suited for students like C for Dummies by Dan Gookin, or C Primer Plus by Prata.(my favorites) The ones from indian authors are pretty pretty good after you know the basics and have a math background, despite the use of conio.h and borland. It's better to read modern books which are lets say around 2010++.
17th Dec 2022, 5:23 PM
arturop200
arturop200 - avatar
0
I use codeblocks .. when i put input like ā€œhow are youā€ and then use the ctrl + c to end the program it returns 0 as the value for nc .. but when i hit enter first for the new line before ctrl + c it gives the right count .. and thanks i think iā€™ll just go with one of the newer books
17th Dec 2022, 5:27 PM
Wasiu Abass
Wasiu Abass - avatar
0
how can a program still runs and print the value if it has already been killed??? Isnt that 0 a message from codeblocks. I remember it adds a message after the program. Im not sure now, I cant test it on windows atm. šŸ«¤
17th Dec 2022, 6:26 PM
arturop200
arturop200 - avatar