我应该期望从“C编程语言”第二版(1991)。里面的例子解释

时间:2021-07-03 15:04:21

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).

我有这本书“C编程语言第二版”(西班牙语版本)已经有几年了,最近我决定尝试一下。这将是我的第一种编程语言,是的,我知道这对乞丐来说并不容易,我喜欢挑战。幸运的是,我找到了一个不能正常工作的例子(见下面)。

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.

下面的代码应该在每次修改nc时计数并显示计数。printf实际上没有打印任何东西。改变EOF到1和键入也不会结束程序。

#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)

问:我是否应该注意到C中的“最近”变化?这本书是1991年的,20年了……(哇我老了)

3 个解决方案

#1


0  

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.

cnictuar的建议正是我的英文平装版《第17版印刷》中的代码片段。也许这个缩进在西班牙语翻译中被搞砸了,但是改变感觉很有趣。

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

C99有很多新功能,但是我很高兴的是:

  • named initializers:

    指定初始化:

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

    在for循环声明:

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

    自动聚合的初始化器可以是非常量表达式:

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

#2


2  

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

如果您想在每次迭代之后打印,您必须用括号括住while。

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

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

如果想要“中断”while(发送EOF),需要CTRL-D或CTRL-Z。

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

无论如何,如果这本书没有指定括号,就像这样缩进:

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

#3


1  

You may want to add a few brakets:

你可能想添加一些brakets:

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

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

这将在每次键入字符时打印nc的值。

#1


0  

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.

cnictuar的建议正是我的英文平装版《第17版印刷》中的代码片段。也许这个缩进在西班牙语翻译中被搞砸了,但是改变感觉很有趣。

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

C99有很多新功能,但是我很高兴的是:

  • named initializers:

    指定初始化:

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

    在for循环声明:

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

    自动聚合的初始化器可以是非常量表达式:

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

#2


2  

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

如果您想在每次迭代之后打印,您必须用括号括住while。

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

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

如果想要“中断”while(发送EOF),需要CTRL-D或CTRL-Z。

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

无论如何,如果这本书没有指定括号,就像这样缩进:

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

#3


1  

You may want to add a few brakets:

你可能想添加一些brakets:

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

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

这将在每次键入字符时打印nc的值。