Home  >  Article  >  Backend Development  >  How Does C Evaluate the Right and Left Operands in Assignment Statements?

How Does C Evaluate the Right and Left Operands in Assignment Statements?

DDD
DDDOriginal
2024-10-29 18:28:45600browse

 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn