Home > Article > Backend Development > How Does C Evaluate the Right and Left Operands in Assignment Statements?
Order of Evaluation in Assignment Statements in C
When assigning a value to a map element, such as mp[10] = mp.size(), the evaluation order of the expression becomes crucial. This particular expression yields an unexpected result of "0 1," which raises questions about the specified order of evaluation in C .
The C standard specifies that the evaluation order is unspecified for certain operations, such as function calls. However, assignment operators like = are sequenced after the right and left operand evaluations, leaving a gap in the specified order. This ambiguity has led to the need for a refined proposal.
The recent C standards proposal (N4228) seeks to address this issue by specifying the evaluation order for certain cases. According to N4228, this specific assignment expression falls under the category of unspecified behavior, where both operand evaluations are unsequenced.
As per the revised version of N4228 (P0145R0), this behavior is now specified. The revised section [expr.ass] states that the right operand of an assignment operator is sequenced before the left operand. Therefore, in the given expression, mp.size() would be evaluated first, followed by the assignment of the result to mp[10]. The updated C 17 standard is expected to clarify this evaluation order.
In summary, the evaluation order of assignment statements remains unspecified in certain cases, but the latest C standards proposal (N4228) aims to refine these rules. The current accepted specification, P0145R3, clarifies that the right operand of an assignment is evaluated before the left operand, resolving the ambiguity in the given expression.
The above is the detailed content of How Does C Evaluate the Right and Left Operands in Assignment Statements?. For more information, please follow other related articles on the PHP Chinese website!