Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is chained assignment in C/C++ undefined behavior?

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.

like image 468
martixingwei Avatar asked Mar 24 '14 12:03

martixingwei


People also ask

What is chained assignment in C++?

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.

Does C have assignment operators?

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.

How does assignment work in C?

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 multiple assignment?

Does C support the same? No.


1 Answers

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.

like image 124
user4815162342 Avatar answered Oct 20 '22 16:10

user4815162342