Home c++ What is the definition, announcement and initialization of the variable

What is the definition, announcement and initialization of the variable

Author

Date

Category

Please explain it is available what definition, ad and initialization variable .
How to distinguish them syntactically. What can I do without what, and what is impossible without anything. I already read several articles, now in my head only porridge


Answer 1, Authority 100%

The following is greatly simplified. It does not pursue the goal to tell in detail all the nuances – it is better to read the standard standard.

Announcement (declaration) of the variable informs the compiler that somewhere maybe in another broadcast unit (very rough, in another CPP file) highlighted Sizeof byte under the storage of a variable of such a type with such a name. Declarations can be written as much as you like in different blocks of code, one on the block.

Definition variable informs the same compiler that the memory under the variable needs to be taken right in this place where this definition is written. It is in this unit of broadcast. The definition of the entire program may be one and only one.

These processes control the qualifiers Static , extern , thread_local and some others.

Most often, the announcement and definition, for example,

occur simultaneously.

int a; // outside of functions - make the compiler create a global variable.
{
  int a; // In the code block - the variable will exist until the end of the block, and the memory will be highlighted on the stack
}

An example of a pure identifier declaration will be announced it with the specifier STATIC inside the structure:

struct a
{
  Static int A; // Announcement A :: A
};

No memory at the moment is not highlighted, but the compiler now knows that it has it. To use this field, you must first define it outside the structure:

int a :: a; // Definition A :: A
int b = a :: a; // Now you can

Using the keyword extern , which just says the compiler that the variable is announced somewhere in another unit of broadcast

//alpha.cpp
 int a;
 //beta.cpp.
 EXTERN INT A; // pointed out that we will use` Int A` out` alpha.cpp`

Name and memory binding itself will occur at the layout stage.

Initialization in the sense of C++ is when the definition and ad are combined with the assignment of the initial value;

{
  int a = 8;
}

Here:

{
  int a;
  a = 8;
}

It will be initialized in the sense of programming practice (the very first record in the variable), but will not be initialization in the sense of C++ – this is a consecutive definition and assignment.

Reading from an uninitialized variable – uniform UB. So it is impossible to write, it is wrong.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions