Monday, 14 May 2012

What is volatile specifier in C?

It is a keyword in C. When a variable declared with volatile specifier in C, indicates the compiler that the value of that variable can be changed at any time. So, compiler loads a copy of the value every time when the program uses that variable.
For Example:
Volatile int i = 10;
If( I ==12)
Printf(“%d”,i);
Else
Printf(“%d”,i);

In the above example, compiler loads the value 10 into variable I and while checking the if condition it loads a copy of value again from that memory location since it is declared as volatile. If it is not declared as volatile, then the compiler becomes blind it doesn’t know the changes made in that variable. Ex: Hardware registry on the PCs, Temperature Sensors. So, the value of i can be changed between the first two statements of the code, if it is it will change the conditional checking.

No comments:

Post a Comment