The volatile qualifier maintains consistency of memory access to data objects. Volatile objects are read from memory each time their value is needed, and written back to memory each time they are changed. The volatile qualifier declares a data object that can have its value changed in ways outside the control or detection of the compiler (such as a variable updated by the system clock). The compiler is thereby notified not to apply certain optimizations to code referring to the object.
Accessing any lvalue expression that is volatile-qualified produces a side effect. A side effect means that the state of the execution environment changes.
References to an object of type "pointer to volatile" may be optimized, but no optimization can occur to references to the object to which it points. An explicit cast must be used to assign a value of type "pointer to volatileT" to an object of type "pointer to T". The following shows valid uses of volatile objects.
volatile int * pvol; int *ptr; pvol = ptr; /* Legal */ ptr = (int *)pvol; /* Explicit cast required */
A signal-handling function may store a value in a variable of type
sig_atomic_t, provided that the variable is declared
volatile. This is an exception to the rule that a
signal-handling function may not access variables with static storage
duration.
Related References