1.

Why do we use the volatile keyword?

Answer»

The volatile keyword is mainly used for preventing a compiler from optimizing a VARIABLE that might change its behaviour unexpectedly post the optimization. Consider a scenario where we have a variable where there is a possibility of its value getting updated by some EVENT or a signal, then we need to tell the compiler not to OPTIMIZE it and LOAD that variable every time it is called. To inform the compiler, we use the keyword volatile at the time of variable declaration.

// Declaring volatile variable - SYNTAX// volatile datatype variable_name;volatile int X;

Here, x is an integer variable that is defined as a volatile variable.



Discussion

No Comment Found