language lawyer - Which object declarations in C cause storage to be reserved (i.e. are definitions)? -
c11 specifies in section 6.7 declarations definitions:
a definition of identifier declaration identifier that:
— object, causes storage reserved object;
[...]
i didn't find comprehensive list of object declarations cause storage reserved. intuitively clear me, wasn't able information out of c11 standard.
there's no definitive list because standard describes definitions , it's not in single place. i'll try sum here. i'll use type int here without qualifiers (like const) consistency.
if add initializer declaration, it's definition:
int x = 1;without initializer, following definitions when they're in function scope:
int x; auto int x; // auto default anyways register int x; // register hint, "storage" static int x; // reserves storage, static durationin file scope, rules bit more complicated; following tentative definitions:
int x; static int x;the wording of standard (§6.9.2p2) is:
a declaration of identifier object has file scope without initializer, , without storage-class specifier or storage-class specifier
static, constitutes tentative definition. if translation unit contains 1 or more tentative definitions identifier, , translation unit contains no external definition identifier, behavior if translation unit contains file scope declaration of identifier, composite type of end of translation unit, initializer equal 0so means "become definitions" if not found refer definition.
with storage class
extern, without initializer, don't have definition:extern int x; // <- not definition
afaik, should complete set of rules. please feel free edit/comment if forgot something.
Comments
Post a Comment