the volatile keyword in C language?

user707549 picture user707549 · Apr 28, 2011 · Viewed 43.2k times · Source

I have a question about volatile in C language.

I read some tutorial but still can not figure out, some says the volatile tells the complier optimizer that operations involving this variable should not be optimized in certain ways. this means anytime the value of a variable is change in register, then the value should affect the memory.

And also some say that volatile mean that the value can be changed by means outside this code.

I can not understand the second saying, so the volatile variable can be changed by means outside of this code? how? and are these two says both right?

Answer

WindsurferOak picture WindsurferOak · Apr 28, 2011

The statement "the value can be changed by means outside of this code" basically means that another program or hardware can update that variable. This is totally possible. One way of thinking of this is by relating this concept to a file that is shared among multiple programs. A file can be opened, written, and read by many programs at once. When you read from a file you want to make sure that you are reading the latest update and not the oldest.

Going back to the volatile keyword, placing volatile before a variable, in effect, does the same thing. It makes sure that what you are reading out of the variable isn't based on the compiler's optimization or an old copy of the variable that your program had. Moreover, the volatile keyword ensures that the variable is fetched from memory on every access. Therefore, both of those statements are correct regarding the volatile keyword.