int a,b;
a++ = b++;
according to me, this is equivalent to:-
a = b;
b++;
a++;
But I get an "lvalue required error". WHY?
int a,b;
a++ = b++;
according to me, this is equivalent to:-
a = b;
b++;
a++;
But I get an "lvalue required error". WHY?
ISO 9989:1999 standard, 6.3.2.1:2:
Except when it is the operand of the sizeof operator, the unary & operator, the ++ operator, the -- operator, or the left operand of the . operator or an assignment operator, an lvalue that does not have array type is converted to the value stored in the designated object (and is no longer an lvalue). If the lvalue has qualified type, the value has the unqualified version of the type of the lvalue; otherwise, the value has the type of the lvalue. If the lvalue has an incomplete type and does not have array type, the behavior is undefined.
The ++ operator evaluates to the value of its argument, not the argument itself. If a=1 and b=2, then it's like saying something roughly equivalent to this.
As 1 is not an lvalue (an lvalue is anything that would be valid on the left hand side of an assignment expression), you're getting an lvalue error. It's also bad practice to put more than one ++ or -- in an expression. There are rules to determine which one occurs first, but there is also undefined behavior in some cases (a = b++ + b++, for example) and it's best to just avoid the whole thing and only use one, or better yet, no ++ or -- operators in expressions.Code:1 = 2; a = a + 1; b = b + 1;
Thanks gaxio and flp1969 -
that was quite instructive.