0
int a = 0;

This code can be read either: declaration of a or definition of a, right?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
doraemon1
  • 113
  • 1
  • 11
  • 3
    I wouldn't use the word "or". That's like saying, "A horse is either an animal or a mammal." – Steve Summit May 15 '22 at 13:09
  • 4
    Do not tag both C and C++ except when asking about differences or interactions between the two languages. Answers generally vary between the two languages, and discriminating the tags helps future users of the site search for answers for the language they want. Pick one language and delete the other tag. – Eric Postpischil May 15 '22 at 13:17
  • @EricPostpischil ok I'll delete c tag. thanks – doraemon1 May 15 '22 at 13:21
  • 1
    Your example is a definition that initialises the variable `a`. All definitions are declarations. The reverse is not true (not all declarations are definitions). – Peter May 15 '22 at 13:28

1 Answers1

5

declaration of a or definition of a right?

Note that every definition is necessarily a declaration in C++, but not the other way round. That is, not every declaration is a definition.

Thus,

int a = 0; // A definition and hence also a declaration

The above is both a definition and also a declaration.


On the other hand, if you were to write:

extern int a; // This is a nondefining declaration

The above statement extern int a; is a declaration that is not a definition (aka a nondefining declaration)


Moreover,

extern int a = 0; // This is a definition and hence also a declaration even when we have used extern!

Note that even though we have used extern in the above statement, the presence of the initializer 0 makes this a definition and hence also a declaration.

Jason
  • 36,170
  • 5
  • 26
  • 60
  • What is `unsigned;`? – Goswin von Brederlow May 15 '22 at 14:58
  • @GoswinvonBrederlow Where? I don't think i have used it(`unsigned`) anywhere in my answer. Or do you see a typo. – Jason May 15 '22 at 15:08
  • No, it's just another example of what the syntax can look like. One that surprises many. – Goswin von Brederlow May 15 '22 at 15:11
  • @GoswinvonBrederlow `unsigned;` isn't valid. Refer to [Why does `int ;` compile fine in C, but not in C++?](https://stackoverflow.com/q/33273550/12002570). – Jason May 15 '22 at 16:01
  • 2
    @AnoopRana The [question you just asked](https://stackoverflow.com/questions/72249815) is actually a useful signpost. It states the question clearly in the title, which is different than the target, and there doesn't appear to be a similar question title on SO. I'd suggest undeleting that question. – cigien May 15 '22 at 16:12
  • @cigien Ok i have undeleted it as per the suggestion. – Jason May 15 '22 at 16:14
  • @AnoopRana Hmm, actually there is a [similarly worded question](https://stackoverflow.com/questions/48192615) already (I clearly didn't search hard enough). Still, no harm in having the question around, there isn't an excessive number of duplicates for that target. – cigien May 15 '22 at 16:25
  • @cigien Also, msvc compiles my [sample snippet](https://godbolt.org/z/cbEK1W95r) without giving any diagnostic but in the [linked](https://stackoverflow.com/a/33273777/12002570) answer it is written that *"A violation of this in either language requires a diagnostic."*. So i think that msvc has a bug. – Jason May 15 '22 at 16:43