What are all the cases when an object declaration is not a definition?
I came up with a tentative answer according to the following quote from C in a Nutshell. Is it correct that an object declaration is a nondefinition declaration, if and only if
when the object declaration is within a function block, it has no initializer, and has storage class specifier
extern
when the object declaration is outside all functions, it has no inititilizer, and
- either has storage class specifier "extern",
- or has no
extern
specifier and the translation unit in which it appears has another declaration for the same identifier which is a definition for the identifier
Equivalently, an object declaration is a nondefinition declaration if and only if
the object declaration has no initializer;
the object declaration also satisfies either of the following:
either the object declaration has storage class specifier
extern
,or the object declaration has no storage class specifier
extern
, and is outside all the functions, and the translation unit in which it appears has another declaration for the same identifier which is a definition for the identifier.
C in a Nutshell says:
An object declaration is a definition if it allocates storage for the object. Declarations that include initializers are always definitions. Furthermore, all declarations within function blocks are definitions unless they contain the storage class specifier
extern
.If you declare an object outside of all functions, without an initializer and without the storage class specifier
extern
, the declaration is a tentative definition. A tentative definition of an identifier remains a simple declaration if the translation unit contains another definition for the same identifier. If not, then the compiler behaves as if the tentative definition had included an initializer with the value zero, making it a definition.
Thanks.