volatile int x;
int y;
int z;
x = 10;
x = 20;
y = x;
z = x;
Answer: the constant 10 is written to x
the constant 20 is written to x
the contents of x is read and written into y
the contents of x is read and written into z
Now, what happens with this code? int x;
int y;
int z;
x = 10;
x = 20;
y = x;
z = x;
One answer is the same as the above. Another valid answer is: the constant 20 is written to x
the constant 20 is written to y
the constant 20 is written to z
Why? Because x is not used between the two assignments, so the first will never be seen. Also, x is not used between it's assignment and the assignment to y, so the compiler can do constant propagation.All volatile does it tell the compiler "all writes must happen, and no caching of reads".