Node:volatile, Next:, Previous:void, Up:More data types


The volatile type qualifier was introduced by the ANSI Standard to permit the use of memory-mapped variables, that is, variables whose value changes autonomously based on input from hardware. One might declare a volatile variable volatile float temperature; whose value fluctuated according to readings from a digital thermometer connected to the computer.

There is another use for the volatile qualifier that has to do with multiprocessing operating systems. Independent processes that share common memory might each change the value of a variable independently. The volatile keyword serves as a warning to the compiler that it should not optimize the code containing the variable (that is, compile it so that it will run in the most efficient way possible) by storing the value of the variable and referring to it repeatedly, but should reread the value of the variable every time. (Volatile variables are also flagged by the compiler as not to be stored in read-only memory.)