Wednesday, November 29, 2006

What is Volatile variable ,can a variable be CONST VOLATILE

The reason to use volatile is to ensure that the compiler generates code to re-load a data item each time it is referenced in your program. Without volatile, the compiler may generate code that merely re-uses the value it already loaded into a register.
Volatile advises the compiler that the data may be modified in a manner that may not be determinable by the compiler. This could be, for example, when a pointer is mapped to a device's hardware registers. The device may independently change the values unbeknownst to the compiler.
A variable should be declared volatile whenever its value can be changed by something beyond the control of the program in which it appears, such as a concurrently executing thread.

Use of volatile
- An object that is a memory-mapped I/O port - An object variable that is shared between multiple concurrent processes - An object that is modified by an interrupt service routine - An automatic object declared in a function that calls setjmp and whose value is-changed between the call to setjmp and a corresponding call to longjmp

Can a variable be both const and volatile?
Yes. The const modifier means that this code cannot change the value of the variable, but that does not mean that the value cannot be changed by means outside this code

Tuesday, November 28, 2006

why +++a works and a++++ not?

a++++ is evaluated as (a++)++. The postfix ++ operator requires an l-value (variable) as it's operand but it does not return an l-value hence the error.
In contrast ++++a is evaluated as ++(++a). The prefix ++ operator requires an l-value just like the postfix operator but unlike the postfix operator it's returning an l-value as well so this works.

why ++a++ doesn't work ?? think................


++a++ should work as prefix++ returns L value and Postfix++ Needs Lvalue (though it Doesnt return L value)

But on Vc++ COmpiler it is not Compiling ;

error C2105: '++' needs l-value


ANSWER : a++++; what abt ++a++


According to Operator Precedence and Associativity, the ++a++ expression is interpreted as ++(a++) and therefore cannot be compiled, since a++ is not an l-value. The order can be changed using parenthesis: (++a)++. It is compiled successfully.