Types of Variable Initialization

There are two types of variable initialization in C++ which are as follows:

1. Static Initialization

Here, the variable is assigned a value in advance. This variable then acts as a constant.

In practice:

  • Static initialization is put in at compile time. Object representations that have already been calculated are saved as part of the program image. The compiler must nonetheless ensure that the initialization occurs before any dynamic initialization if it chooses not to do it.
  • Variables that need to be zero-initialized are stored in the portion of an object file, executable, or assembly language code that contains statically allocated variables that are declared but have not been assigned a value yet, which takes up no space on the disc and zeroes out by the operating system when the program is loaded.

2. Dynamic Initialization

Here, the variable is assigned a value at the run time. The value of this variable can be altered every time the program is run. Moreover, dynamic initialization is of 3 kinds i.e.

  1. Unordered Dynamic Initialization
  2. Partially-Ordered Dynamic Initialization
  3. Ordered Dynamic Initialization

Different Ways to Initialize a Variable in C++

Variables are arbitrary names given to the memory location in the system. These memory locations are addressed in the memory. In simple terms, the user-provided names for memory locations are called variables. Additionally, a data type is used to declare and initialize a variable.

 

Suppose we want to save our marks in memory. Now, these marks will get saved at a particular address in the memory. Now, whenever these marks will be updated, they will be stored at a different memory address. Thus, to facilitate the fetching of these memory addresses, variables are used. Variables are names given to these memory locations. The memory location referred to by this variable holds a value of our interest. Now, these variables once declared, are assigned some value. This assignment of value to these variables is called the initialization of variables.

Similar Reads

Types of Variable Initialization

There are two types of variable initialization in C++ which are as follows:...

Different ways of Initializing a Variable in C++

There are 7 methods or ways to initialize a variable in C++:...