What should I expect from The C Programming Language 2nd Edition (1991). Example inside explanation

I had this book "C Programming Language Second Edition" (Spanish version) for several years, I recently decided that I would do it. This will be my first programming language, and I know that it is not easy for beginners, I like problems. The thing, fortunately, I found an example of incorrect operation (see below) .

The following code should count and display a counter every time nc changes. the printf thing doesn't actually print anything. Changing the EOF to 1 and entering it will also not end.

#include <stdio.h> main() { long nc; nc = 0; while(getchar() != EOF) ++nc; printf("%ld\n", nc); } 

Question: Should I be aware of any β€œrecent” changes in C? . This book is from 1991, 20 years old ... (wow I'm getting old)

+4
source share
3 answers

cnictuar's suggestion is exactly what this piece of code looks like in my English paperback version, 17th print. Maybe the indentation was spoiled by translating into Spanish, but it seems to me that this is ridiculous.

There are many new features in C99 , but the ones I really like are the following:

  • Initializers:

     struct {int a, b, c, d;} s = { .a = 1, .c = 3, 4, .b = 5}; 
  • in for loops:

     for (int i; i<foo; i++) { ... } 
  • Initializers for automatic aggregates may be inconsistent expressions:

     void foo(int n) { int arr[n]; } 
0
source

If you want to print after each iteration, you must surround it with brackets.

  while(getchar() != EOF) { ++nc; printf("%ld\n", nc); } 

If you want to β€œsplit” the time (send EOF), you need CTRL-D or CTRL-Z.

In any case, if the brackets are not indicated in the book, back off like this:

  while(getchar() != EOF) ++nc; printf("%ld\n", nc); 
+2
source

You can add several commands:

 while(getchar() != EOF) { ++nc; printf("%ld\n", nc); } 

This will print the nc value each time you enter a character.

+1
source

All Articles