# What is different between constant and variable in C++?

Variable and constant are two commonly used mathematical concepts. Simply put, a variable is a value that is changing or that have the ability to change. A constant is a value which remains unchanged.

For example, if you have a program that has a list of 10 radii and you want to calculate the area for all of these circles. To find the area of these circles, you'll write a program that will have a variable that will store the value of PI and this value will not change throughout the program. Such values can be declared as a constant.

In the same example, if you're calculating the area in a loop, you can use the same variable to temporarily store the value of the area and print it and then reuse it for some other calculation. The code for the above will look something like −

float area;
const float PI = 3.141;
for(int i = 0; i < 10; i++) {
area = PI * radii[i] * radii[i]; // Calculate area
cout << area;  // Print area
}

The value of PI remains same throughout the life of this program.

Samual Sam

Learning faster. Every day.