0

This is in reference to C++. This question I have goes back about 6 months, when I used to think that a declaration was:

int a;

And a definition was:

a = 5;

Also, that:

int a = 5;

was both a declaration and definition.

I've now come to understand that:

int a;

Is a definition, not just a declaration. (I don't know why).

Also, there are terms such as assigning, and initialising which add further jargon to the issue.

So here is my current understanding (and please correct anything):

int a;    // Declaration and definition

int a = 5 // Declaration, definition, assignation and initialisation.

a = 5;    // Initialising (if for the first time),
          // assigning (if for subsequent times),
          // defining (not sure about this one).

I have read quite a bit on this topic but am still confused. Can someone explain exactly what each is? I know that in such cases there are philosophical disputes, like is zero an even number, or even a number. Haha, but can someone try? Thanks.

phuclv
  • 27,258
  • 11
  • 104
  • 360
Zebrafish
  • 9,170
  • 2
  • 28
  • 78
  • "initialize" means that you provide an initial value (special case is when you explicitly provide an empty list, that still counts as initialization). – M.M Oct 04 '15 at 07:07

1 Answers1

0

A declaration may introduce one or more names into a translation unit or redeclare names introduced by previous declarations.

A definition is a declaration in some specific circumstance. (ie, a declaration with initializer). This is because they have to meet the One definition rule. All definitions are declarations, but not vice-versa.

Initialization gives an initial value to a variable in its definition. Initialization will also happens in other situation (eg. function argument passing)

Assignment is used to assign a new value to an already existing object.

In your specific case

int a;    // definition, default-initialization if it's local, zero-initialization otherwise
int a = 5; // definition, copy-initiaization
a = 5;   // assignment

Also, the following is a declaration but not a definition

extern int i;  // declaration but not definition
Carousel
  • 698
  • 4
  • 13
  • Why this old question appears in the active questions? I'm not the one who digs it up. – Carousel Aug 09 '16 at 05:02
  • I asked this ages when I was first learning. The meanings seem rather arbitrary and depend on the standard I think. For example, is an int declared in a class a definition? I don't think it is, just a declaration. But outside a class in global space it is. – Zebrafish Sep 05 '16 at 14:22