Hi All,
I want share some information about volatile keyword. So just check the following example:
main()
{
volatile int x = 10;
volatile int y = 20;
int z;
z = fun(x,y);
z = fun(x,y);
z = fun(x,y);
}
int fun(volatile int a,volatile int b)
{
int c;
c = a+b;
return c;
}
Now a days compilers go for code optimization. So instead of calling the function every time they will take last returned value to avoid stack overhead.
But if the values of x and y are dependent on the some external conditions (for example in real time like temperature, pressure) which will change every often, then compiler should take latest values of x and y.
So volatile tells compiler the don’t optimize the code and take the updated values from the stack. Volatile is used only when the variables are changing without user’s knowledge.
| With Regards, | | | | | | | | | | | | | | | | |
| Santosh Kumar B. | ||||||||||||||||
No comments:
Post a Comment