C++ Variable Initialization

Whenever we need to store data in computer memory we have to use variables. Variables are actually the symbolic name of the location somewhere in the computer memory.  The variables can hold different values of same data type at different time during the execution of a program. There are many ways for C++ Variable initialization. 

To declare a variable of a data type we use following syntax

Data-type First-Variable, Second-Variable, Third-Variable;

For example

int x, y, z, a;

Initialization of variable

If we check the value of the above variables then there will be some garbage value present at those memory locations. Therefore when we have to assign values or initialize the values of the variables we use the

int x=40, y=0;

There is another way as well to initialize the value of the variable. This method is known as construction initialization. In this method the value is enclosed in the parenthesis. E.g.

int x (40), y (0);

Both ways are equivalent.


A variable that does not change its value throughout execution of the programs are called constant. Any attempt to change the value of constant will prompt an error message. The keyword constant is used before the variable initialization

constant float pi=3.1415;

in the above statement pi is a constant whose value 3.1315 and it cannot be changed throughout the program.

Type Conversion

The process in which a pre-defined variable is converted into another type is called type conversion. There are two types of conversions in C++. These are implicit conversion and explicit conversion/type casting.

Implicit conversion

In the implicit type conversion, a data type can be mixed in the expression. In this process two or more different types are encountered in the same expression and the lower type variable is converted into higher type variable. In the following example the type of x is converted into float for multiplication. After the multiplication the result is converted to double type for the assignment of the result to a.

int x = 5;
double y;
float z = 8.5;
y = x * z;

The order of the data types in the conversation process is given in the following table

Order of data types
Data type
long double






Explicit conversion/Type casting

 In this type of conversion the type of the variable is temporary changed from pre-defined to new one. The type casting is done only on the right hand side of the assignment statement. To clarify type casting look at the following example

double bonus, TotalPay;
float BasicSalary;
TotalPay = static_cast<double>( BasicSalary)+bonus;

It can be noted that initially the variable BasicSalary is defined as float but for the multiplication it has been converted to double data type.