本文介绍了语句z = ++ x || ++ y& ++ z的C运算符优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究运算符的优先级,但无法理解x的值如何变为2yz的值如何变为1

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 1

x=y=z=1;

z=++x||++y&&++z;

计算结果为

x=2 y=1 z=1

推荐答案

++具有比||更高的优先级,因此,分配的整个RHS都归结为x的增量,并对真值求值值(1).

++ 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

这是因为++x的计算结果为true,并且第二个分支未执行. ++x2,在布尔上下文中,其值为true或1. z取值为1,为您提供观察到的最终状态.

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.

这篇关于语句z = ++ x || ++ y& ++ z的C运算符优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 13:25