Ignoring the types of variables, is expression like a=b=c
has defined behavior in both C and C++?
If so, can any one give me official evidence, like quotes from the standard, please?
P.S. I searched the chained assignment but everything I got is associativity, but I didn't find any text about that in the C99 standard. Maybe I did it wrong? hoping anyone can help me.
In C++ the assignment operator returns an lvalue referring to the left operand while in C it returns the value of the left operand after the assignment,111) but is not an lvalue. It means that in C++ the following code is valid.
Assignment Operators in C/C++ Assignment operators are used to assigning value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value.
An assignment operation assigns the value of the right-hand operand to the storage location named by the left-hand operand. Therefore, the left-hand operand of an assignment operation must be a modifiable l-value. After the assignment, an assignment expression has the value of the left operand but is not an l-value.
Does C support the same? No.
According to §6.5.16 (3) of C99:
An assignment expression has the value of the left operand after the assignment, [...]
Together with right-associativity of evaluation, and assuming non-volatile a
, b
, and c
, it means that a = b = c
is equivalent to a = (b = c)
, and again equivalent to b = c; a = b
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With