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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Initialize a vector in C++ (5 different ways)
- Different ways to Initialize all members of an array to the same value in C
- Different ways to declare variable as constant in C and C++
- Can we access global variable if there is a local variable with same name?
- Internal static variable vs. External static variable with Examples in C
- How to Initialize and Compare Strings in Java?
- How to initialize Array of objects with parameterized constructors in C++
- Efficient ways to compare a variable with multiple values
- Different ways for Integer to String Conversions In Java
- Different ways of Method Overloading in Java
- Array of Strings in C++ (5 Different Ways to Create)
- Different ways of Reading a text file in Java
- Different ways to delete elements in std::map (erase() and clear())
- Preventing Object Copy in C++ (3 Different Ways)
- Ways to express a number as product of two different factors
- Print system time in C++ (3 different ways)
- Different ways to use Const with Reference to a Pointer in C++
- 8 different ways to Add Two Numbers in C/C++
- How to Count Variable Numbers of Arguments in C?
- Variable length arguments for Macros
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

