0

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

  1. when the object declaration is within a function block, it has no initializer, and has storage class specifier extern

  2. 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

  1. the object declaration has no initializer;

  2. 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.

Tim
  • 1
  • 141
  • 372
  • 590
  • `int f(int a);` is a declaration, but not a definition, for example. You're overthinking it. – Federico klez Culloca Aug 17 '17 at 12:50
  • The second point in the second point, if the declaration have no `extern` and there is no later definition, then it *is* a definition. – Some programmer dude Aug 17 '17 at 12:51
  • @Tim ok, than simply `int a;` – Federico klez Culloca Aug 17 '17 at 12:55
  • It's not **that** simple! Look at the duplicate and better answers than mine should be added there as well :) –  Aug 17 '17 at 12:56
  • @FelixPalmen: Thanks. The linked one asks when an object declaration is a definition. That is exactly the complement of mine. Theorectically answering one will answer the other. But it is better to have both complementary views explicitly, than just one. Do you think that my tentative answer is correct according to your reply there? – Tim Aug 17 '17 at 12:57
  • 1
    @Tim both ask for the exact rules, it doesn't matter from which end you start. It's an exact duplicate. I still upvoted here because answers are surprisingly complicated :) –  Aug 17 '17 at 12:58
  • @Tim you have to at least change in the second part "*the object declaration has no initializer*" to "*the object declaration has an initializer*". –  Aug 17 '17 at 13:00
  • @FelixPalmen Thanks. it was my typo. I also corrected another place. How about the update? – Tim Aug 17 '17 at 13:02

0 Answers0