开发者

What should i expect from "The C Programming Language" 2nd edition(1991). Example inside explaining

开发者 https://www.devze.com 2023-03-15 19:40 出处:网络
I have had this book \"The C Programming Language Second Edition\" (Spanish Version) for a few years, I recently decided i would give it a shot. It would be my first programming language and yes I kno

I have had this book "The C Programming Language Second Edition" (Spanish Version) for a few years, I recently decided i would give it a shot. It would be my first programming language and yes I know It's not easy for begginers, i like challenges. Thing is luckily i found a not-working correctly example (see below).

The following code is supposed to count and display the count everytime nc is modified. thing is printf is actually not printing anything. Changing EOF to 1 and typing it won't end the program either.

#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... (wow I'm getting old)


If you want to print after each iteration you have to surround the while with brackets.

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

If you want to "break" the while (send EOF) you need CTRL-D or CTRL-Z.

Anyway, if the book doesn't specify brackets, indent it like this:

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


You may want to add a few brakets:

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

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


cnictuar's suggestion is exactly what that code snippet looks like in my English paperback edition, 17th printing. Maybe the indent got screwed up in the Spanish translation, but it feels like a funny thing to change.

There are a lot of new features in C99, but the ones that I'm really happy about are:

  • named initializers:

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

    for (int i; i<foo; i++) { ... }
    
  • Initializers for auto aggregates can be non-constant expressions:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号