Which object declarations in C cause storage to be reserved (i.e. are definitions)?

前端 未结 1 1934
醉梦人生
醉梦人生 2020-12-06 07:27

C11 specifies in section 6.7 which declarations are also definitions:

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

相关标签:
1条回答
  • 2020-12-06 08:16

    There's no definitive list because the standard just describes what are definitions and it's not in a single place. I'll try to sum it up here. I'll only use the type int here without any qualifiers (like const) for consistency.

    • If you add an initializer to a declaration, it's always a definition:

      int x = 1;
      
    • Without an initializer, the following are definitions when they're in function scope:

      int x;
      auto int x;        // auto is the default anyways
      register int x;    // register is just a hint, but would be "storage" as well
      static int x;      // also reserves storage, but with static duration
      
    • In file scope, the rules are a bit more complicated; the following are tentative definitions:

      int x;
      static int x;
      

      The wording of the standard (§6.9.2p2) is:

      A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with the storage-class specifier static, constitutes a tentative definition. If a translation unit contains one or more tentative definitions for an identifier, and the translation unit contains no external definition for that identifier, then the behavior is exactly as if the translation unit contains a file scope declaration of that identifier, with the composite type as of the end of the translation unit, with an initializer equal to 0

      so this means that they eventually "become definitions" if they are not found to refer to another definition.

    • With the storage class extern and without an initializer, you don't have a definition:

      extern int x;     // <- not a definition
      

    AFAIK, this should be the complete set of rules. Please feel free to edit/comment if I forgot something.

    0 讨论(0)
提交回复
热议问题