Possible Duplicate:
Why was mixing declarations and code forbidden up until C99?
有几个问题与此警告相关:ISO C90 forbids mixed declarations and code,但它们并没有首先解决为什么这是在C90标准中的问题。
为什么要这样规定?

最佳答案

我知道的最大的原因是它简化了语言语法和语法分析器。
在前面声明的情况下,代码块必须看起来像

{
    <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

关于c - ISO C90禁止混合使用声明和代码-为什么,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14632395/

10-11 11:41