- 论坛徽章:
- 2
|
回复 17# KBTiller
- function-definition
函数定义的组成在 C89 与 C99 大体相同:
-- C89 6.7.1 Function definitions
Syntax
function-definition:
declaration-specifiersopt declarator declaration-listopt compound-statement
-- C99 6.9.1 Function definitions
Syntax
function-definition:
declaration-specifiers declarator declaration-listopt compound-statement
都是这几个部分, C89 首部是可选的, 所以可以万恶地省略返回类型……
重点是最后的 compound-statement。
也就是平时所说的"函数体" body。
-- C++ 8.4 Function definitions
“函数体” 的概念在 C++ 中更明显:
function-definition:
decl-specifier-seqopt declarator ctor-initializeropt function-body
decl-specifier-seqopt declarator function-try-block
function-body:
compound-statement
也许 "函数体" 的说法是从C++传入的?
总之, return_type function_name( parameters ) 之后的 , 是一个 compound-statement 。
- compound-statement
-- C89 - 6.6.2 Compound statement, or block
Syntax
Compound statement
{ declaration-listopt statement-listopt }
declaration-list:
declaration
declaration-list declaration
statement-list:
statement
statement-list statement
C89 区分 declaration 与 statement , 且 compound-statement 由 0 或多个 declaration 后跟 0 或多个 statement 组成。
所以声明必须在 compound-statement 中的前部, 在任一语句之前。
-- C99 - 6.8.2 Compound statement
Syntax
compound-statement:
{ block-item-listopt }
block-item-list:
block-item
block-item-list block-item
block-item:
declaration
statement
C99 将 declaration 与 statement 统称为 block-item , compound-statement 由 0 或多个 block-item 组词。
所以 declaration 与 statement 可以交替出现。
-- C++ 6.3 Compound statement or block
1 So that several statements can be used where one is expected, the compound statement (also, and equivalently, called “block”) is provided.
compound-statement:
{ statement-seqopt }
statement-seq:
statement
statement-seq statement
C++ 里又没有这样统称 (只是将 compound-statement 称为 block ) 。
- statement
-- C89 6.6, C99 6.8
statement:
labeled-statement
compound-statement
expression-statement
selection-statement
iteration-statement
jump-statement
-- C++ - 6 Statements
statement:
labeled-statement
expression-statement
compound-statement
selection-statement
iteration-statement
jump-statement
declaration-statement
try-block
C89/C99 将 6 种语句统称为 statement , 然后再将 statement 与 declaration 统称为 block-item 。
C++ 在 statement 中增加 declaration-statement, C++:statement 与 C99:block-item 是相同的(不考虑 try-block) 。
所以C++中声明也是可以随处丢的。
C89 若想将声明延后, 只能在组成函数体的 compound-statement 中使用子 compound-statement , 并在其首部写声明。 |
|