The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

Constants and Volatile

Constants

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.

Constant vs Volatile in C

Constant vs Volatile

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.

Constant Program Using Keyword

Let's write a C program to demonstrate the purpose of constant variable

constant.c
#include <stdio.h>
int main()
{
const int i = 16;
i = i + 1;
printf("The value of i = %d ",i);
return 0;
}

Note:

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.

Constant Program Without Using Keyword

Let's write a C program to demonstrate the purpose of constant variable without using keyword.

noconstant.c
#include <stdio.h>
#define i 16
int main()
{
printf("The value of i = %d ",i);
return 0;
}
  • The value of i = 16

Note:

Variable i is constant here because we defined using preprocessor directive. Thus, incrementing its value by any number is impossible throughout the entire program.

Volatile Program Using Keyword

Let's write a C program to demonstrate the purpose of volatile variable

volatile.c
#include <stdio.h>
int main()
{
volatile int j = 1;
j = j + 1;
printf("The value of j = %d ",j);
return 0;
}
  • The value of j = 2

Note:

Variable 'j' is declared as volatile variable. Thus, incrementing its value by any number is possible throughout the entire program.

Volatile Program Without Using Keyword

Let's write a C program to demonstrate the purpose of volatile variable

novolatile.c
#include <stdio.h>
int main()
{
int j = 1;
j = j + 1;
printf("The value of j = %d ",j);
return 0;
}
  • The value of j = 2

Note:

Variable 'j' is volatile because by default all variables are volatile. Thus, incrementing its value by any number is possible throughout the entire program.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report