Volatile keyword says the compiler that no optimiztion on the variable.
The volatile keyword acts as a data type qualifier.
The volatile qualifier alters the default behaviour of the variable and does not attempt to optimize the storage referenced by it.
- Martin Leslie
volatile means the storage is likely to change at anytime and be changed but something outside the control of the user program. This means that if you reference the variable, the program should always check the physical address (ie a mapped input fifo), and not use it in a cacheed way.
- Stephen Hunt
volatile datatype variable;
To declare a variable volatile, include the keyword volatile before or after the data type in the variable definition. For instance both of these declarations will declare foo to be a volatile integer:
volatile int foo;
int volatile foo;
Now, it turns out that pointers to volatile variables are very common. Both of these declarations declare foo to be a pointer to a volatile integer:
volatile int * foo;
int volatile * foo;
Volatile pointers to non-volatile variables are very rare (I think I’ve used them once), but I’d better go ahead and give you the syntax:
int * volatile foo;
And just for completeness, if you really must have a volatile pointer to a volatile variable, then:
int volatile * volatile foo;
Finally, if you apply volatile to a struct or union, the entire contents of the struct/union are volatile. If you don’t want this behavior, you can apply the volatile qualifier to the individual members of the struct/union.
Use of volatile variable
A variable should be declared volatile whenever its value could change unexpectedly.
We use volatile variables in
Jones, Nigel. “Introduction to the Volatile Keyword” Embedded Systems Programming, July 2001