简体   繁体   中英

Is `function-definition` a `declaration`?

In the C11 standard

6.9 External definitions

Syntax

translation-unit: external-declaration translation-unit external-declaration external-declaration: function-definition declaration

where

6.9.1 Function definitions

Syntax

function-definition: declaration-specifiers declarator declaration-listopt compound-statement declaration-list: declaration declaration-list declaration

and

6.7 Declarations

Syntax

declaration: declaration-specifiers init-declarator-listopt ; static_assert-declaration declaration-specifiers: storage-class-specifier declaration-specifiersopt type-specifier declaration-specifiersopt type-qualifier declaration-specifiersopt function-specifier declaration-specifiersopt alignment-specifier declaration-specifiersopt init-declarator-list: init-declarator init-declarator-list , init-declarator init-declarator: declarator declarator = initializer

...

A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that:

  • for an object, causes storage to be reserved for that object;
  • for a function, includes the function body;
  • for an enumeration constant, is the (only) declaration of the identifier;
  • for a typedef name, is the first (or only) declaration of the identifier.

Is function-definition a declaration ?

Is a declaration using function-specifier a function-definition or something else? ("6.7.4 Function specifiers" introduces definitions of inline functions, so it looks like function-definition is declaration ?)

In external-declaration , why is function-definition singled out instead of being included in declaration ?

Thanks.

A function-definition is not a declaration in the formal C grammar. This is evident from the Syntax clauses throughout the standard, which present the formal grammar.

A function-definition is a declaration in the sense that it does declare the identifier for a function, as well as defining the function. This is stated in C 2018 6.9 5, which says “An external definition is an external declaration that is also a definition of a function (other than an inline definition) or an object…”

The grammar of function definitions needs to be specified separately from other declarations simply because they have a different form than other declarations. However, the tokens in the formal grammar could have been named non-function-definition-declaration and function-definition-declaration instead of declaration and function-definition . The names are of no consequence to the definition of the grammar, and the fact that declaration was chosen for what is actually a subset of declarations is simply an artifact of naming.

A definition of an identifier is a declaration for that identifier ref

The notion of declaration does not correspond exactly to the grammar symbol declaration . An external-declaration (the grammar symbol, in italics) is not necessarily a declaration , but an external declaration (in a Roman font) is obviously a kind of a declaration. I believe there is no other sensible interpretation of the standard.

The grammar is designed with machines (parsers) in mind, and the English text is designed for humans, so there are discrepancies here and there.

C doesn't allow functions to be defined inside a block. All function definitions must be at the top-level of a translation unit. Since the syntax for a translation unit is a sequence of external-declarations, it's logical that we'll find function definitions only in the expansion of external-declaration .

Of course, the top-level of a translation unit is not restricted to function definitions. It can also include declarations, including function declarations. But it cannot include (executable) statements, which in the context of the C grammar are called statements .

In short, the C grammar contains three mutually exclusive categories of what we might call "statement-like things":

  • statement
  • declaration
  • function-definition

Block lists contain either of the first two categories; the top-level of a translation unit contains either of the second two. There's no context in which all three are acceptable.

These categories are not absolutes. Another language (or grammar) might use different wording. (In C++, declarations are statements, for example, and function definitions can occur in more contexts.)

In normal language, it's pretty common to say that a function definition is a declaration. Clearly, it declares that an identifier names a function. Even the standard occasionally uses "declaration" in this sense. But it's not in the grammatical category of declaration .

Similarly, a declaration is not in the grammatical category of statement , although in informal language, it's common to include declarations. (That's a mistake, though: declarations cannot be labelled, not even with case labels.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM