The volatile type qualifier

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 or by another program). This prevents the compiler from optimizing code referring to the object by storing the object's value in a register and re-reading it from there, rather than from memory, where it may have changed.

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 volatile T" 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 */

C only 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.

An item can be both const and volatile. In this case the item cannot be legitimately modified by its own program but can be modified by some asynchronous process.