I would like to know the difference between volatile and const.Pls. explain it with an example.
Andy, In general that might be true. The defaults on Keil, however, give an "unmodifiable lvalue" error C183.
"Modifying a variable definition with const means that the compiler will flag as an error any attempt to directly modify it later in the code." I don't think it has to give an error - it could be just a warning?
"CONST is used to declare the constants i.e the value of this data type will not be changed through out the program execution." Not exactly. const can be used in function parameter declarations - this doesn't mean that the parameter value will not change throughout the program execution! eg, http://www.keil.com/support/man/docs/c51/c51_strrpos.htm
Hi, CONST is used to declare the constants i.e the value of this data type will not be changed through out the program execution. VOLATILE, Normal this data type will be used to declare the variable for timercount & interrupt status because the value of this specific types can't be expected any time. Krishna M.
Thanx a lot jay. krishna
There's no easy way to make a direct comparison between volatile and const, although I suppose in some sense they are exact opposites. Modifying a variable definition with const means that the compiler will flag as an error any attempt to directly modify it later in the code. So, something like this:
void somefunc(void) { const int x = 5; x = 7; }
int somefunc(void) { int y; y = 7; return (y * 2); }
View all questions in Keil forum