Assignment Is Right Associative
Unlike the other binary operators, assignment is right associative:
int ival, jval;ival = jval = 0; // ok: each assigned 0
Because assignment is right associative, the right-most assignment, jval = 0, is the right-hand operand of the left-most assignment operator. Because assignment returns its left-hand operand, the result of the right-most assignment (i.e., jval) is assigned to ival.
Each object in a multiple assignment must have the same type as its right-hand neighbor or a type to which that neighbor can be converted (§ 4.11, p. 159):
int ival, *pval; // ival is an int; pval is a pointer to intival = pval = 0; // error: cannot assign the value of a pointer to an intstring s1, s2;s1 = s2 = "OK"; // string literal ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access