ISO C90禁止混合声明和代码 - 为什么

时间:2013-01-31 18:13:35

标签: c ansi c89

  

可能重复:
  Why was mixing declarations and code forbidden up until C99?

有一些与此警告相关的问题:ISO C90 forbids mixed declarations and code但是他们没有解决为什么这首先出现在C90标准中。

那么 - 为什么这个规定?

1 个答案:

答案 0 :(得分:4)

我所知道的最大原因是它简化了语言语法和解析器。

预先声明,代码块必须看起来像

{
    <declarations>
    <stmts>
}

因此简化了<stmts>的定义,因为它不必涉及声明。反过来,这简化了解析器,因为它只需要消除块开始时声明的语句。

事实上,代码块的这个特定定义已编入标准:

3.6.2 Compound statement, or block

Syntax

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

          declaration-list:
                  declaration
                  declaration-list declaration

          statement-list:
                  statement
                  statement-list statement