Open In App

Different Ways to Initialize a Variable in C++

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Variable in C++

 

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.

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 of Initializing a Variable in C++

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

Method 1: Declaring and Initializing a Variable

int a = 5;

Method 2: Initializing a Variable using Parenthesis

int a (5) ;

Yes, they’re the same. On the other hand, for a class type, they’re different.

Example:

struct A {
    A(int);
};
A a(5);
// This statement is to construct a;

Method 3: Initializing a variable using braces

int a{5} ;

Method 4: Declaring a variable using auto class

auto a = 5;

‘auto’ is a keyword that tells the compiler the type of the variable upon its initialization.

Method 5: Declaring and Initializing a variable through the ‘auto’ keyword with parenthesis

auto a (5);

Method 6: Declaring and Initializing a variable through the ‘auto’ keyword with braces

auto a{5};

Method 7: Dynamic Initialization

int a;
cin>>a;

Example:

C++




// C++ program to demonstrate the different variable
// initialization methods in C++
#include <iostream>
using namespace std;
  
int main()
{
    // initialization
    int var1 = 5;
    int var2(5);
    int var3{ 5 };
    auto var4 = 5;
  
    // printing variable
    cout << "Method 1 Variable: " << var1 << endl;
    cout << "Method 2 Variable: " << var2 << endl;
    cout << "Method 3 Variable: " << var3 << endl;
    cout << "Method 4 Variable: " << var4 << endl;
    return 0;
}


Output

Method 1 Variable: 5
Method 2 Variable: 5
Method 3 Variable: 5
Method 4 Variable: 5

These are all the different ways in which a variable can be defined in C or C++. The ways are similar for all fundamental variables but the way to initialize a variable of derived data type changes accordingly. 

Different derived data types have an altogether different way of getting their variable initialized and hence can be explored in detail while diving into the about of that particular data type.



Last Updated : 11 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads