【问题标题】:C grammar compound statement [duplicate]C语法复合语句[重复]
【发布时间】:2014-05-14 20:54:02
【问题描述】:

我正在查看 K&R 上的 C 语法,我发现了这个:

compound-statement:
{ declaration-list opt statement-list opt }

declaration-list:
declaration
declaration-list declaration

statement-list:
statement
statement-list statement

这意味着我们不能在语句之后进行声明。但是我经常这样做:

#include <stdio.h>

int main()
{
  printf("Lets use a new block");
  {
    int a=1;
    printf("%d",a);
    int b=3;
    printf("%d",b);
  }
  return 0;
}

此代码编译时没有警告和错误。我没有正确理解语法吗?

【问题讨论】:

  • 这是重复的,是的,过去是规则,现在已经放宽了。
  • 启用更多警告,你会得到你所期望的(gcc -- 凭记忆 -- -pedantic 应该这样做)。
  • K&R 是一本很棒的书,但它不是最近的一本。

标签: c context-free-grammar


【解决方案1】:

要获得您想要的错误,请将这些标志传递给 gcc:

-std=c90 -pedantic-errors

GNU 扩展以及更新的 C 标准允许在范围内的其他语句之后进行声明。

【讨论】:

    【解决方案2】:

    你理解语法很好。然而,自 K&R 时代以来,C 语言已经进步,现在语法接受交错的声明和语句。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      相关资源
      最近更新 更多