A variable should be qualified as a “volatile”,when there is possibility that it can be changed by something beyond the control of the program in which it appears.
Let there is data type P.
then this data type P can be declared like
volatile P;

The volatile qualifier like int a = 3, declares and initialize int a and value of a will be always read from memory.

Use of volatile

  • Memory map i/o port
  • A variable that is shared between concurrent process
  • A object/variable modified by interupt service routine
  • A local variable declared in function that calls setjmp and whose value is changed between call to setjmp and coresponding call to longjmp.

 

  • #A local variable declared in function that calls setjmp and whose value is changed between call to setjmp and corresponding call to longjmp.

volatile variable isn't affected by optimization. so value of b is the last value variable assigned.
<reference from Book(advance test in c and embedded system programming , Ashok k Pathak>
http://www.youtube.com/watch?v=ImHW6I0BoEs