Volatile


Declaring a variable as volatile

ANSI standard defines another qualifier volatile that could be used to tell explicitly the compiler that a variable's value may be changed at any time by some external sources. For example :

volatile int value;

The value of 'value' may be altered by some external factors even if it does not appear on the left-hand side of an assignment statement. When we declare a variable as volatile, the compiler will examine the value of the variable each time it is encountered to see whether any external alteration has changed the value.

Note that the value of any variable declared as volatile can also be modified by its own program. If we wish that the value is not changed by the program as soon as it is changed by any other process, we may declare the variables as variable const and volatile as in the figure below:

volatile const int value = 100;