Different Ways to Initialize a Variable in C++
Last Updated :
11 Mar, 2023
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.
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.
- Unordered Dynamic Initialization
- Partially-Ordered Dynamic Initialization
- 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;
}
OutputMethod 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.
Similar Reads
Different Ways to Initialize a List in C++ STL Initializing a list means assigning some initial values to the list elements. In this article, we will learn different methods to initialize the list in C++. Letâs start from the easiest method:The easiest way to initialize a list is by passing the initial values inside an initializer list to its co
3 min read
Inline Variables in C++ 17 An inline variable in C++ is a variable that is declared using an inline specifier. It is an exception to one definition having multiple definitions across various translation units. Inline variables have an external linkage when not declared as static. Syntaxinline data_type variable_name = initial
3 min read
Designated Initializers in C++ 20 With C++20, we get a convenient way of initializing data members. The new feature is called Designated Initializers and it might be familiar to C programmers. In other words, Designated Initializers are a new feature that has been introduced in C++20. It allows developers or programmers to initiate
5 min read
Different methods to initialize a Linked List Like arrays, a Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at a contiguous location; the elements are linked using pointers. They include a series of connected nodes. Here, each node stores the data and the address of the next node. There are two method
15+ min read
std::initializer_list in C++ 11 The std::initializer_list class template was added in C++ 11 and contains many built-in functions to perform various operations with the initializer list. It provides member functions like a size(), begin(), end(), and constructor to construct, iterate, and access elements of the initializer list. T
6 min read
How to Declare & Initialise a String in different Programming languages? To declare and initialize a string in different programming languages, you can use language-specific syntax and functions. Below are examples in C++, C#, Python, JavaScript, and Java, with explanations for each language: How to Declare & Initialise a String in C++Steps: Include the necessary hea
2 min read
How to Initialize a Static std::map<int, int> in C++ In C++, std::map<int, int> is a commonly used container that stores key-value pairs. There are scenarios where we may want to initialize a static std::map with predefined key-value pairs that remain constant throughout the program's execution. In this article, we will learn different methods t
4 min read
How to Initialize an Array in Java? An array in Java is a linear data structure that is used to store multiple values of the same data type. In an array, each element has a unique index value, which makes it easy to access individual elements. We first need to declare the size of an array because the size of the array is fixed in Java
5 min read
Zero Initialization in C++ Setting the initial value of an object to zero is called zero initialization. Syntax: static T object; Tt = {} ; T {} ; char array [n] = " "; Zero initialization is performed in the following situations:- Zero is initialized for every named variable with static or thread-local storage duration that
3 min read
How to avoid Compile Error while defining Variables Variables: A variable is the name given to a memory location. It is the basic unit of storage in a program. The value stored in a variable can be changed during program execution. A variable is only a name given to a memory location, all the operations done on the variable effects that memory locati
3 min read