Programs that deal directly with hardware often have data elements whose value is controlled by processes outside the direct control of the program itself. For example, a program might contain a variable updated by the system clock. An object should be declared volatile when its value might be changed in ways outside either the control or detection of the compiler. The volatile keyword is a directive to the compiler that it should not perform optimizations on such objects.
volatile int v; // v is a volatile int int *volatile vip; // vip is a volatile pointer to int volatile int *ivp; // ivp is a pointer to volatile int // vivp is a volatile pointer to volatile int volatile int *volatile vivp; int *ip = &v; // error: must use pointer to volatile *ivp = &v; // ok: ivp is pointer to volatile vivp = &v; // ok: vivp is volatile pointer to volatile