Saturday, July 30, 2011

(PT) Difference between const and volatile

 

What is the difference between const and volatile variable ? In fact, everyone will be clear from const, but it is really difficult to understand about volatile keyword. 
A const variable is the variable, which can't be modified by the compiler. If you try to modify it, then compiler will give an error message. Suppose, we have a const variable as below. 
const int i = 10; i = 20 // error - compiler throws the variable can't be modified. 
But in case of volatile keyword, the variable declared as volatile tells the compiler that the variable can be modified anytime other than the compiler. 
Suppose, we are returning a volatile variable from a function. int func(){  volatile int i;  i = 10;   return (i*2);}
Here we are expecting fun() function to return 20. but as we are declaring i as volatile, so between return (return (i*2) ) and assignment statement (i = 10), it might get modified to some different value and return value might differ from 20. 
Please clarify if anything i explain and explain in details to understand better. What is use of volatile keyword ? In some blog, i have seen - use of volatile keyword is mostly used in multi-threaded application and interrupt status application. but how is it used in these kind of application. Please clarify this too ? regards - Ravi 

[Non-text portions of this message have been removed]

__._,_.___
Recent Activity:
To unsubscribe : programmers-town-unsubscribe@yahoogroups.com

.

__,_._,___

No comments: