Different ways to initialize a variable in C/C++ - GeeksforGeeks

15 Oct, 2019
Variables are arbitrary names given to a memory location in the system. These memory locations addresses in the memory. 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 initialization of variables.
Initialization of a variable is of two types:
Static Initialization: Here, the variable is assigned a value in advance. This variable then acts as a constant.
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 being run.
Method 1 (Declaring the variable and then initializing it)
int a;   
a = 5; 

Method 2 (Declaring and Initializing the variable together):
int a = 5;   
Method 3 (Declaring multiple variables simultaneously and then initializing them separately)
int a, b;       
a = 5;         
b = 10;       

Method 4 (Declaring multiple variables simultaneously and then initializing them simultaneously)
int a, b;     
a = b = 10;  

int a, b = 10, c = 20;   

Method 5 (Dynamic Initialization : Value is being assigned to variable at run time.)
int a;
printf("Enter the value of a");
scanf("%d", &a);   

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. For 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 which tells the compiler the type of the variable upon its initialization.
Method 5 (Declaring and Initializing a variable through ‘auto’ keyword with parenthesis)
auto a (5);  
Method 6 (Declaring and Initializing a variable through ‘auto’ keyword with braces)
auto a{5};   
Method 7 (Dynamic Initialization)
int a;
cin>>a;           

These are all the different ways in which a variable can be defined in C or C++. The ways are similar for all fundamental variable 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 in the all about of that particular data type.
divyanshsinghxi
CBSE - Class 11
school-programming
C Language
C++
School Programming
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
rand() and srand() in C/C++
Left Shift and Right Shift Operators in C/C++
fork() in C
Command line arguments in C/C++
Function Pointer in C
Inheritance in C++
Map in C++ Standard Template Library (STL)
C++ Classes and Objects
Operator Overloading in C++
Socket Programming in C/C++