What does the volatile keyword mean in C++?


volatile means two things −

- The value of the variable may change without any code of yours changing it. Therefore whenever the compiler reads the value of the variable, it may not assume that it is the same as the last time it was read, or that it is the same as the last value stored, but it must be read again.

- The act of storing a value to a volatile variable is a "side effect" which can be observed from the outside, so the compiler is not allowed to remove the act of storing a value; for example, if two values are stored in a row, then the compiler must actually store the value twice.

As an example −

i = 2;
i = i;

The compiler must store the number two, read the variable i, store the variable that it read into i.
You can find more details on the volatile keyword here − https://www.geeksforgeeks.org/understanding-volatile-qualifier-in-c/

Updated on: 10-Feb-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements