Is (--i == i++) an Undefined Behavior?

后端 未结 8 2044
离开以前
离开以前 2020-12-22 11:06

this question is related to my previous problem. The answer I got was \"It is an Undefined behavior.\"

Please anyone explain:

  • What is an undef
8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-22 11:45

    It's undefined in C, but well-defined in C#:

    From C# (ECMA-334) specification "Operator precedence and associativity" section (§14.2.1):

    • Except for the assignment operators and the null coalescing operator, all binary operators are left- associative, meaning that operations are performed from left to right. [Example: x + y + z is evaluated as (x + y) + z. end example]

    So --i is evaluated first, changing i to 4 and evaluating to 4. Then i++ is evaluating, changing i to 5, but evaluating to 4.

提交回复
热议问题