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

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.

Different ways of initializing a variable in C

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);   

Different ways of initializing 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. 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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : divyanshsinghxi