Simon P Simonson said:
OK, but what does it mean to access a? If a is stored in a memory
location (possibly memory mapped hardware) is it enough to load a into a
register? What if a is already in cache?
Here's what the standard says (C99 6.7.3p6):
An object that has volatile-qualified type may be modified
in ways unknown to the implementation or have other unknown
side effects. Therefore any expression referring to such an
object shall be evaluated strictly according to the rules of
the abstract machine, as described in 5.1.2.3. Furthermore,
at every sequence point the value last stored in the object
shall agree with that prescribed by the abstract machine, except
as modified by the unknown factors mentioned previously. What
constitutes an access to an object that has volatile-qualified
type is implementation-defined.
with a footnote:
A volatile declaration may be used to describe an object
corresponding to a memory-mapped input/output port or an object
accessed by an asynchronously interrupting function. Actions
on objects so declared shall not be ‘‘optimized out’’
by an implementation or reordered except as permitted by the
rules for evaluating expressions.
On the other hand, if you just apply "volatile" to a local variable,
the compiler can reasonably know that there's no magic going on behind
the scenes. It's not clear whether the compiler is permitted to take
advantage of this knowledge; I'd say it probably isn't.