Problem with example 1.5.2 in K & R in C

I teach myself C with K & R, and I am puzzled by one example in the book. I compile the code exactly as it was written in the example, but it does not do what the authors say it will be. It is assumed that the program is designed to count characters. The code below is as follows:

#include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc=0; while (getchar() != EOF) ++nc; printf("%ld\n", nc); } 

To compile it, I will replace main () with int main (). But I believe that this is not the case. The program compiles and works fine. But it simply does not account for the characters as it was written. Am I missing something? Could something change in the way modern compilers view code examples such as this since the book was written? Any help that good people can provide on this bulletin board would be greatly appreciated.

Best Dan

+4
source share
4 answers

The program displays only the number of characters after reading the "end of file". Using interactive input, you can generate the "end of file" using ctrl + d (at least on * NIX, you don't know about Windows). Knowing this, the program works correctly here.

+5
source

Besides the return value of main, it looks fine.

Do you execute Ctrl D (Unix) or Ctrl Z (Windows) at the end of the input if you enter values ​​from the keyboard?

+5
source

Although the other answers are technically correct, I believe that this example (1.5.2) and the next (1.5.3) are pedagogically confusing. Just google "character counting 1.5.2" and you will find many others who have been picked up by this example, just like the OP did. The reason this is so confusing is that there is no explanation in the text on how to generate the EOF symbol interactively, and the previous examples output the results immediately after entering "return". So any newbie in C would suggest that a program in 1.5.3 should do the same ...

I would suggest the following alternative code that gives the expected result:

 #include <stdio.h> #define EOL '\n' main() { long nc; int c; nc = 0; while ((c = getchar()) != EOF) { ++nc; if (c == EOL) { /* Print number of input characters (not including return character) */ printf("%ld\n", nc-1); nc = 0; } } } 

The only C element that is not already explained in the text is the if , which is actually explained in the next section (1.5.3). I hope this small alternative example serves to help others keen on the original example from K & R. A good β€œExercise 1.7b” would be to study the differences between the two versions and explain that they produce the same results (after reading about Ctrl D / Ctrl Z from other answers).

+5
source

It is also worth noting that Ctrl + z (which will be displayed as ^ Z in the console) cannot simply be entered anywhere in the console; you should enter it as the first input of your last line string / text / characters. for instance

Initial input image Ctrl + z

As you can see in this example, I typed in random text and hit enter after each line. NOW IMPORTANT !!! When you press enter in the last line, it will call EOF (End-of-File), and you will get the rest of the executable code as originally intended.

Fully executed code

Note:

  • Despite the fact that Ctrl + z appears as ^ Z, it is not considered a symbol of the program, but many times you press it.
  • Also, characters after ctrl + z are not taken into account.
  • Enter is counted by this program.

Source: EOF on Windows command line does not terminate input stream

0
source

All Articles