Assignment operator sequencing in C11 expressions

前端 未结 3 1914
既然无缘
既然无缘 2020-12-03 11:40

Introduction

The C11 standard (ISO/IEC 9899:2011) has introduced a new definition of side effect sequencing within an expression (see related question). The se

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-03 12:15

    But why is i = ++i + 1 undefined according to C11's terminology?

    C11 says that the side effect on left i is sequenced but not the value computations (evaluations) of left and right i.
    It is obvious that the side effect on LHS will take place after the evaluation of expressions on LHS and RHS.
    To explain this a better example could be

    int i = 1;
    i = i++ + 3;
    

    (First let's assume that this example will not invoke UB). Now the final value of i could be 4 or 2.
    Case 1.
    Left i is fetched and then it is incremented and 3 is added to it and finally 4 is assigned to i.
    Case 2.
    Left i is fetched and then 3 is added to it and then 4 is assigned to i and finally i is incremented. In this case the final value of i is 2.
    Although the side effect on left i is sequenced the final value stored to i is not defined, i.e it is not necessarily by the assignment and hence the side effect on i is unsequenced.

提交回复
热议问题