简体   繁体   中英

order of evaluation of comparison operator?

I have learnt that logical operator are guaranteed that their evaluation are from left-to right but I was wondering what are the order of evaluation of comparison operator. For instance expression1 < expression2 in other words is it guaranteed that expression1 will be first evaluated before expression2 .

According to the standard:

J.1 Unspecified behavior

The following are unspecified:
....

— The order in which subexpressions are evaluated and the order in which side effects take place, except as specified for the function-call () , && , || , ?: , and comma operators (6.5).

Generally speaking, the order of evaluation of subexpressions within an expression is undefined.

The only place where there is an order, ie sequence points, is the || (logical OR), && (logical AND), , (comma), and ?: (ternary) operators.

In the case of && , if the expression on the left evaluates to false (ie 0), the result is known to be false and the right side is not evaluated. Similarly for || if the expression on the left evaluates to true (ie not 0), the result is known to be true and the right side is not evaluated.

For the ternary operator, the conditional is evaluated first. If it evaluates to true then only the middle part is evaluated, otherwise only the third part is evaluated.

For the comma operator, the left side is evaluated first, then the right side.

From the C standard :

6.5.13.4 Unlike the bitwise binary & operator, the && operator guarantees left-to-right evaluation; there is a sequence point after the evaluation of the first operand. If the first operand compares equal to 0, the second operand is not evaluated.

...

6.5.14.4 Unlike the bitwise | operator, the || operator guarantees left-to-right evaluation; there is a sequence point after the evaluation of the first operand. If the first operand compares unequal to 0, the second operand is not evaluated.

...

6.5.15.4 The first operand is evaluated; there is a sequence point after its evaluation. The second operand is evaluated only if the first compares unequal to 0; the third operand is evaluated only if the first compares equal to 0; the result is the value of the second or third operand (whichever is evaluated), converted to the type described below. If an attempt is made to modify the result of a conditional operator or to access it after the next sequence point, the behavior is undefined.

....

6.5.17.2 The left operand of a comma operator is evaluated as a void expression; there is a sequence point after its evaluation. Then the right operand is evaluated; the result has its type and value. If an attempt is made to modify the result of a comma operator or to access it after the next sequence point, the behavior is undefined.

No, the spec does not mention the order of evaluation for the operand of relational operators. It's unspecified.

Just to add, relational operators are left-to-right associative .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM