Differences between initialization, definition, variable declaration

After reading the question , I know the differences between declaration and definition. Does this mean that the definition is equal to declaration plus initialization?

+45
c ++ c terminology
Apr 28 '14 at 15:48
source share
4 answers

Declaration

The declaration, as a rule, refers to the introduction of a new name in the program. For example, you can declare a new function by describing its “signature”:

void xyz(); 

or declare an incomplete type:

 class klass; struct ztruct; 

and, just as important, declare an object:

 int x; 

The C ++ standard describes in §3.1 / 1 as:

An advertisement (section 7) may enter one or more names in a translation unit or the redesign names entered by previous advertisements.

Definition

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

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

In particular, the C ++ standard defines it in § 3.1 / 1 as:

A declaration is a definition if it does not declare a function without specifying the function body (8.4), contains the extern specifier (7.1.1) or the binding specification 25 (7.5), and neither the initializer nor the function body, it declares a static data member in the class definition (9.2 , 9.4), this is a class name declaration (9.1), this 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 declaration that is not a function definition declarator, or it is typedef declaration (7.1.3), alias declaration (7.1.3), declaration using statements (7.3.3), a static_assert declaration (section 7), an attribute declaration (section 7), an empty declaration (section 7), or a using directive (7.3.4).

Initialization

Initialization means “assigning” a value at build time. For a general object of type T it is often found in the form:

 T x = i; 

but in C ++ it could be:

 T x(i); 

or even:

 T x {i}; 

with c ++ 11.

Conclusion

Does this mean that the definition is equal to declaration plus initialization?

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

 int x; 

This definition is without initialization. Instead, this is an initialization definition:

 int x = 0; 

In a certain context, it makes no 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 : a definition does not automatically mean a declaration plus initialization.

+54
Apr 28 '14 at 16:03
source share

The declaration says that "this thing exists somewhere":

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

The definition says that "this thing exists here, remember it":

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

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

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

Sometimes this is possible at the point of declaration:

 struct T { static int baz = 42; }; 

& hellip; but it concerns more complex functions.

+22
Apr 28 '14 at 16:25
source share

For C, at least for C11 6.7.5:

An ad defines the interpretation and attributes of a set of identifiers. An identifier definition is a declaration for that identifier, which:

  • for an object, causes the storage to be reserved for this object;

  • for a function includes the body of the function

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

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

Per C11 6.7.9.8-10:

The initializer determines the initial value stored in the object ... if an object that has automatic storage is not initialized explicitly, its value is undefined.

So, in general, the declaration introduces an identifier and provides information about it. For a variable, a definition is a declaration that allocates memory for this variable.

Initialization is the specification of the initial value that must be stored in the object, which does not necessarily coincide with the first when you explicitly assigned it a value. A variable matters when you define it, whether or not you explicitly give it a value. If you do not explicitly give it a value, and the variable has automatic storage, it will have an initial value, but this value will be undefined. If it has static storage, it will be initialized implicitly depending on the type (for example, pointer types get initialized with null pointers, arithmetic types are initialized to zero, etc.).

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

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

You define it (and therefore also declare it, since definitions are declarations), but do not initialize it. Therefore, definition is not equal to declaration plus initialization.

+4
Apr 28 '14 at 16:15
source share

"That is, means that the definition is equal to the declaration plus initialization."

Not necessarily, your declaration may be without initializing any variable, for example:

  void helloWorld(); //declaration or Prototype. void helloWorld() { std::cout << "Hello World\n"; } 
+1
Apr 28 '14 at 16:18
source share



All Articles