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.

No comments: