eg:
x = 0;
If x not volatile , the compiler will see that it is not in use and will probably exclude it (either the operator x = 0; or even the variable itself) completely from the generated code as an optimization.
However, the volatile keyword precisely prevents compiler execution. It basically tells the code generator: "Do you think this variable is / does not, donโt think, I need it." Thus, the compiler will treat the volatile variable as accessible, and it will issue the actual code that matches the expression.
user529758
source share