Besides the form others have said, the volatile keyword usually prevents compiler form optimizations. In certain memory-displayed registers where the value of the registers continues to change (for example, the RTC clock register), a variable keyword is used. Take a look at this example:
RTC_CLOCK _time; TIME _currentTime = _time ; while(_currentTime - _time >= 100) {
If we do not add the volatile keyword before TIME, this code will look like this: _currentTime - _time = 0, and the compiler will not consider the while loop under it.
RTC_CLOCK _time; TIME _currentTime = _time ;
To prevent this, we should use the volatile keyword over time.
Raulp
source share