84

After reading the question, I know the differences between declaration and definition. So does it mean definition equals declaration plus initialization?

Community
  • 1
  • 1
Tony
  • 5,213
  • 1
  • 31
  • 47

4 Answers4

106

Declaration

Declaration, generally, refers to the introduction of a new name in the program. For example, you can declare a new function by describing it's "signature":

void xyz();

or declare an incomplete type:

class klass;
struct ztruct;

and last but not least, to declare an object:

int x;

It is described, in the C++ standard, at §3.1/1 as:

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

Definition

A definition is a definition of a previously declared name (or it can be both definition and declaration). For example:

int x;
void xyz() {...}
class klass {...};
struct ztruct {...};
enum { x, y, z };

Specifically the C++ standard defines it, at §3.1/1, as:

A declaration is a definition unless it declares a function without specifying the function’s body (8.4), it contains the extern specifier (7.1.1) or a linkage-specification25 (7.5) and neither an initializer nor a function- body, it declares a static data member in a class definition (9.2, 9.4), it is a class name declaration (9.1), it is an opaque-enum-declaration (7.2), it is a template-parameter (14.1), it is a parameter-declaration (8.3.5) in a function declarator that is not the declarator of a function-definition, or it is a typedef declaration (7.1.3), an alias-declaration (7.1.3), a using-declaration (7.3.3), a static_assert-declaration (Clause 7), an attribute- declaration (Clause 7), an empty-declaration (Clause 7), or a using-directive (7.3.4).

Initialization

Initialization refers to the "assignment" of a value, at construction time. For a generic object of type T, it's often in the form:

T x = i;

but in C++ it can be:

T x(i);

or even:

T x {i};

with C++11.

Conclusion

So does it mean definition equals declaration plus initialization?

It depends. On what you are talking about. If you are talking about an object, for example:

int x;

This is a definition without initialization. The following, instead, is a definition with initialization:

int x = 0;

In certain context, it doesn't make sense to talk about "initialization", "definition" and "declaration". If you are talking about a function, for example, initialization does not mean much.

So, the answer is no: definition does not automatically mean declaration plus initialization.

Shoe
  • 70,092
  • 30
  • 150
  • 251
  • 16
    Not quite: `int x;` is a definition as well as a declaration. – Angew is no longer proud of SO Apr 28 '14 at 16:06
  • 3
    @Angew, thanks, I've added a more complete *definition*. – Shoe Apr 28 '14 at 16:16
  • I think the question that OP mentioned in his post answers definition and declaration pretty well. But the original question was `So does it mean definition equals declaration plus initialization`. – Tahlil Apr 28 '14 at 16:34
  • @Tahlil, added a conclusion. Thanks for making me notice. – Shoe Apr 28 '14 at 16:45
  • 3
    @Tony I think (please someone correct me if Im wrong so I learn) 'extern int x' makes it just a declaration, meaning that definition is found elsewhere. Whilst 'int x' actually defines it albeit it gets appointed with random garbage value, unless you initialize it specifically like 'int x = 5'. – brat Nov 08 '17 at 16:36
43

Declaration says "this thing exists somewhere":

int foo();       // function
extern int bar;  // variable
struct T
{
   static int baz;  // static member variable
};

Definition says "this thing exists here; make memory for it":

int foo() {}     // function
int bar;         // variable
int T::baz;      // static member variable

Initialisation is optional at the point of definition for objects, and says "here is the initial value for this thing":

int bar = 0;     // variable
int T::baz = 42; // static member variable

Sometimes it's possible at the point of declaration instead:

struct T
{
   static int baz = 42;
};

…but that's getting into more complex features.

Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989
  • Very well explained, except that initialization is a bit more complex than that. (This wouldn't be C++ if it were that simple.) Initialization includes things like the zero initialization of variables with static lifetime, and default constructors, as well as what you show. (And to add to the confusion: in C, initialization can be the first time the variable is assigned to; e.g. in statements like "taking the value of an uninitialized variable". It wouldn't surprise me if some of this also slipped into C++.) – James Kanze Apr 28 '14 at 17:09
  • Oh, and there are also special cases where C++ permits the specification of the initialization in a declaration. – James Kanze Apr 28 '14 at 17:17
  • 1
    @JamesKanze: Decided to keep it super simple for these purposes – Lightness Races in Orbit Apr 28 '14 at 17:40
  • This doesn't make sense, static member variables allocate memory. – GT 77 Jan 22 '21 at 11:37
7

For C, at least, per C11 6.7.5:

A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that:

  • for an object, causes storage to be reserved for that object;

  • for a function, includes the function body;

  • for an enumeration constant, is the (only) declaration of the identifier;

  • for a typedef name, is the first (or only) declaration of the identifier.

Per C11 6.7.9.8-10:

An initializer specifies the initial value stored in an object ... if an object that has automatic storage is not initialized explicitly, its value is indeterminate.

So, broadly speaking, a declaration introduces an identifier and provides information about it. For a variable, a definition is a declaration which allocates storage for that variable.

Initialization is the specification of the initial value to be stored in an object, which is not necessarily the same as the first time you explicitly assign a value to it. A variable has a value when you define it, whether or not you explicitly give it a value. If you don't explicitly give it a value, and the variable has automatic storage, it will have an initial value, but that value will be indeterminate. If it has static storage, it will be initialized implicitly depending on the type (e.g. pointer types get initialized to null pointers, arithmetic types get initialized to zero, and so on).

So, if you define an automatic variable without specifying an initial value for it, such as:

int myfunc(void) {
    int myvar;
    ...

You are defining it (and therefore also declaring it, since definitions are declarations), but not initializing it. Therefore, definition does not equal declaration plus initialization.

Crowman
  • 22,866
  • 5
  • 38
  • 54
  • In C++, initialization is significantly different. (C also has the equivalent of zero initialization of objects with static lifetime.) – James Kanze Apr 28 '14 at 17:16
  • @JamesKanze: Yes, unfortunately this question is tagged with both C and C++, which isn't very helpful on those points where they differ. – Crowman Apr 28 '14 at 17:20
  • Quite. For the compatible types, the _intent_ (at least originally) in C++ was that they behave the same as in C; in fact, I think they do still behave the same in all of the cases where the languages are themselves compatible. But the wording used to define this behavior is quite different. – James Kanze Apr 29 '14 at 08:13
1

"So does it mean definition equals declaration plus initialization."

Not necessarily, your declaration might be without any variable being initialized like:

 void helloWorld(); //declaration or Prototype.

 void helloWorld()
 {
    std::cout << "Hello World\n";
 } 
Shoaib
  • 157
  • 2
  • 9