As the name suggests, the variable is something that can be changed. It may be a real number or a string. A variable is also called a memory address.
It is not always possible to predict whether a variable is constant or volatile. There is no fixed rule to say whether a variable is constant or volatile.
For example, an integer variable I can be considered constant and a string variable s can be volatile.
Let us consider the following statements:
int i=10,j,s;
i=20;
j=i+10;
s="Hello";
The variables i,j, and s are used in this program. The first statement will assign value to i. So, it is considered constant. However, the second statement assigns the value 20 to i. So, it is considered volatile.
So, it can be said that a variable can be both constant and volatile. If the value of the variable is changing then it is considered volatile.
What is Constant Variable?
If you have any experience of working on C programming language, then you might have heard the term of constant variable. And if not, then I will describe it briefly.
A constant variable is an integer or character that does not change in value while executing the program. We use constant variables when we need a fixed value for the purpose of calculation.
Let me show you an example, if you have to calculate the average of three numbers, then you can make it more efficient by using the constant variable.
int sum = a + b + c;
double avg = (double)sum / 3.0;
Here, we can see that sum is used as a constant variable and it does not change in value while executing the program. So, it will be more efficient to calculate the average of the three values.
There is a very common mistake that people do while writing the code. If you have used a variable which is constant, then you cannot use it in the loop.
If you need to use a constant variable in the loop, then you need to declare it first.
For Example,
const int i = 10;
for (int i = 0; i < 10; i++) {
}
Now, we cannot use the variable i here because it is a constant variable. Similarly, if you use the variable i in the if statement, then you will get a warning. If you need to use the constant variable in the loop or if statement, then you need to declare it first.
What is Variable?
A variable is a value that changes in value. When we declare a variable, it is assigned a value.
Let’s take an example,
int i = 10;
int j = i * 2;
In the above line of code, we are declaring two variables. One is named i and another is named j. Now, we have to assign a value to the variable. The value of i is 10 and the value of j is 50.
Here, we can see that the value of i is changing. When we are going to use the variable, it is important to know that the value of the variable is changing. If we have a variable which is constant, then it will remain the same in value.
Variable is a term used to describe an object that stores information. Variables can be either local or global. Local variables are created within a function or method. Global variables are defined outside the functions and methods.
Types of variables
- Constant
- Variable
- Volatile
Constant: Constant means the same value throughout the program. It will always retain its value and does not change over time.
Example of a constant:
int x = 2; // x is a constant
Global variable: Global variable is used to access the data from the outside of the program. It is available to all the functions and methods.
Example of a global variable:
int x = 2; // x is a global variable
Volatile: Volatile variable is a temporary value which changes each time the function is called.
Example of a volatile variable:
int x = 2; // x is a volatile variable
Variable:
It is a temporary value which changes each time the function is called.
Example of a variable:
int x = 2; // x is a variable
So, we know that a variable is a temporary value which changes over time. But it is also true that a variable can be both a constant and a volatile.
So, is a variable can be both a constant and a volatile in C programming?
Yes, a variable can be both a constant and a volatile. The only condition is that the variables should have different values for each call of the function.
Conclusion:
If you have used a constant variable, then you need to use it in the loop, and if you are using a variable, then it will be changed in value. So, it is better to use the constant variable. In a nutshell, we can say that a variable can be both constant and volatile. However, it cannot be said that a variable is constant if it is changing.