Apart form what others have said, volatile keyword is generally to prevent the compiler form doing the optimization.
In certain memory mapped registers where the value of the registers keep on changing (e.g. an RTC clock register) volatile key word is used.
Take a look at this example :
RTC_CLOCK _time;
TIME _currentTime = _time ;
while(_currentTime - _time >= 100)
{
//Do something
}
//rest of the code
If we do not append the volatile keyword before TIME this code will be like this as _currentTime - _time = 0 and the compiler will not consider the while loop below it.:
RTC_CLOCK _time;
TIME _currentTime = _time ;
//rest of the code
to prevent this we have to use the volatile keyword with the TIME.