| Pointer | ||||
| Array | ||||
| Bit-field | ||||
| Atomic types(C11) | ||||
(C23) | ||||
(C99) | ||||
| Alignment specifiers | ||||
| Storage duration and linkage | ||||
| External and tentative definitions | ||||
| Static assertions | ||||
| Attributes(C23) |
At the top level of atranslation unit (that is, a source file with all the #includes after the preprocessor), every C program is a sequence ofdeclarations, which declare functions and objects withexternal or internal linkage. These declarations are known asexternal declarations because they appear outside of any function.
externint n;// external declaration with external linkageint b=1;// external definition with external linkagestaticconstchar*c="abc";// external definition with internal linkage int f(void)// external definition with external linkage{int a=1;// non-externalreturn b;} staticvoid x(void)// external definition with internal linkage{}
Objects declared with an external declaration have staticstorage duration, and as such cannot useauto orregister specifiersexcept thatauto can be used for type inference(since C23). The identifiers introduced by external declarations havefile scope.
Contents |
Atentative definition is an external declaration without an initializer, andeither without astorage-class specifier or with the specifierstatic.
Atentative definition is a declaration that may or may not act as a definition. If an actual external definition is found earlier or later in the same translation unit, then the tentative definition just acts as a declaration.
int i1=1;// definition, external linkageint i1;// tentative definition, acts as declaration because i1 is definedexternint i1;// declaration, refers to the earlier definition externint i2=3;// definition, external linkageint i2;// tentative definition, acts as declaration because i2 is definedexternint i2;// declaration, refers to the external linkage definition
If there are no definitions in the same translation unit, then the tentative definition acts as an actual definition thatempty-initializes the object.
int i3;// tentative definition, external linkageint i3;// tentative definition, external linkageexternint i3;// declaration, external linkage// in this translation unit, i3 is defined as if by "int i3 = 0;"
Unlike theextern declarations, which don't change the linkage of an identifier if a previous declaration established it, tentative definitions may disagree in linkage with another declaration of the same identifier. If two declarations for the same identifier are in scope and have different linkage, the behavior is undefined:
staticint i4=2;// definition, internal linkageint i4;// Undefined behavior: linkage disagreement with previous lineexternint i4;// declaration, refers to the internal linkage definition staticint i5;// tentative definition, internal linkageint i5;// Undefined behavior: linkage disagreement with previous lineexternint i5;// refers to previous, whose linkage is internal
A tentative definition with internal linkage must have complete type.
staticint i[];// Error, incomplete type in a static tentative definitionint i[];// OK, equivalent to int i[1] = {0}; unless redeclared later in this file
Each translation unit may have zero or one external definition of every identifier withinternal linkage (astatic global).
If an identifier with internal linkage is used in any expression other than anon-VLA,(since C99)sizeof,_Alignof(since C11)(until C23),alignof(since C23), ortypeof(since C23), there must be one and only one external definition for that identifier in the translation unit.
The entire program may have zero or one external definition of every identifier withexternal linkage.
If an identifier with external linkage is used in any expression other than anon-VLA,(since C99)sizeof,_Alignof(since C11)(until C23),alignof(since C23), ortypeof(since C23), there must be one and only one external definition for that identifier somewhere in the entire program.
Inline definitions in different translation units are not constrained by one definition rule. See | (since C99) |
Seestorage duration and linkage for the meaning of the keywordextern with declarations at file scope
Seedefinitions for the distinction between declarations and definitions.
Tentative definitions were invented to standardize various pre-C89 approaches to forward declaring identifiers with internal linkage.