:What is the function of the keyword “Volatile”?

What is the function of the keyword “Volatile”?

Hey @Kash-Moulik-3715574511847721
volatile means two things −

  • The value of the variable may change without any code of yours changing it. Therefore whenever the compiler reads the value of the variable, it may not assume that it is the same as the last time it was read, or that it is the same as the last value stored, but it must be read again.

  • The act of storing a value to a volatile variable is a “side effect” which can be observed from the outside, so the compiler is not allowed to remove the act of storing a value; for example, if two values are stored in a row, then the compiler must actually store the value twice.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.