Operator precedence in C for the statement z=++x||++y&&++z

后端 未结 4 1567
感情败类
感情败类 2021-01-16 09:48

I was studying operator precedence and I am not able to understand how the value of x became 2 and that of y and z is

4条回答
  •  自闭症患者
    2021-01-16 10:47

    ++ has higher priority than ||, so the whole RHS of the assignment boils down to an increment of x and an evaluation to a truth value (1).

    z = ++x         ||  ++y&&++z;
        truthy (1)     never executed
    

    This is because ++x evaluates to true and the second branch is not executed. ++x is 2 which, in a boolean context, evaluates to true or 1. z takes the value of 1, giving you the observed final state.

提交回复
热议问题