The ones who are crazy enough to think they can change the world are the ones who do.- Steve Jobs
Things which are all unchangable are said to be constant whereas things which are all changable are said to be volatile. The following diagram clearly represents the relationship between constant and volatile.
In the following diagram sealed box contains a variable i which is initialized by 16 at the time of declaration which indicates that one cannot change the value of variable i after initialization whereas opened box contains a varibale j which is intialized by 30 at the time of declaration, open box indicates the one can change the value of variable j at anytime.
The following table represents the constant against volatile
Constant | Volatile |
---|---|
Constant variables are unchangable. | Volatile variables are changable. |
Constant variable can be created by using the keyword const. | Volatile varibale can be created by using the keyword Volatile. |
For example const int i = 16 is the way of declaring and initializing constant variable. | For example volatile int j = 30 is the way of declaring and initializing volatile variable. |
Constant variable can only be initialized at the time of declaration. | Volatile varibale can be initialized at anytime. |
By default, all variables are not constant. | By default, all variables are volatile. |
The const variable is otherwise known as Read Only Variable. | The volatile variable is otherwise known as Read/Write Variable. |
Another way to achieve constant variable by the use of #define preprocessor directive e.g.) #define a 5. | Another way to achieve volatile variable by the use of nothing e.g.) int a = 5 is volatile. |
Let's write a C program to demonstrate the purpose of constant variable
#include <stdio.h> int main() { const int i = 16; i = i + 1; printf("The value of i = %d ",i); return 0; }
Variable i is constant here. Thus, incrementing its value by any number is impossible throughout the entire program. So the above program will cause an error.
Let's write a C program to demonstrate the purpose of constant variable without using keyword.
#include <stdio.h> #define i 16 int main() { printf("The value of i = %d ",i); return 0; }
Variable i is constant here because we defined using preprocessor directive. Thus, incrementing its value by any number is impossible throughout the entire program.
Let's write a C program to demonstrate the purpose of volatile variable
#include <stdio.h> int main() { volatile int j = 1; j = j + 1; printf("The value of j = %d ",j); return 0; }
Variable 'j' is declared as volatile variable. Thus, incrementing its value by any number is possible throughout the entire program.
Let's write a C program to demonstrate the purpose of volatile variable
#include <stdio.h> int main() { int j = 1; j = j + 1; printf("The value of j = %d ",j); return 0; }
Variable 'j' is volatile because by default all variables are volatile. Thus, incrementing its value by any number is possible throughout the entire program.
We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.
© Copyright 2019