0

Character Counting in C

I can not understand how this code can count the input. I know the logic of EOF but the part I don't understand is how come the output is the count of the character. As far as I know this code's output should be 1 then 2 and incrementing 1 each time input is registered. I am a beginner and it is an example from a book. Thanks in advance. #include <stdio.h > main() { long nc; nc = 0; while (getchar() != EOF) { ++nc; printf(" %ld \n", nc); } }

8th Jan 2020, 6:42 PM
mert aydın
1 Réponse
+ 3
getchar() method takes a charecter from standard input. So with a while loop, If you input a char, it consumes and in loop nc is incremented. And it repeats until press enter or no more chars left to read.. EOF means end of file.. So the nc variable value tells how many characters you read...
8th Jan 2020, 6:58 PM
Jayakrishna 🇮🇳