简体   繁体   English

更改C中运算符的优先级

[英]Changing the precedence of the operators in C

As precedence of && is more (is this the suitable adj.?) than precedence of ||, I wrote something like this: 因为&&的优先级比(||的优先级)更多(这是合适的调整吗?),所以我写了这样的内容:

if (a || b && c ){ ....

I guess the program will first find b && c , let's call it d , and then find a || d 我猜程序会先找到b && c ,让我们称它为d ,然后找到a || d a || d , right? a || d ,对吗?

I want the the program to check "a" statement first, and then if it is wrong; 我希望程序首先检查“ a”语句,然后检查是否错误; check b and c. 检查b和c。 Is something like this possible? 这样的事情可能吗?

Logical expressions in C are subject to short-circuit evaluation . C中的逻辑表达式需要进行短路评估 Now, as you noted, 正如您所指出的,

a || b && c

is the same as 是相同的

a || (b && c)

because of precedence. 因为优先。

But a is evaluated first, because evaluation order is left-to-right. 但是首先评估a ,因为评估顺序是从左到右。 Because of short-circuit evaluation, only if a is false does the program then go on to evaluate b && c . 由于存在短路评估,因此只有当a为假时程序才继续评估b && c So, your code already does what you want it to. 因此,您的代码已经完成了您想要的工作。

I want the the program to check "a" statement first, and then if it is wrong; 我希望程序首先检查“ a”语句,然后检查是否错误; check b and c. 检查b和c。 Is something like this possible? 这样的事情可能吗?

Yes. 是。 You are going right. 你说对了。

a || b && c a || b && c will be parsed as a || (b && c) a || b && c将被解析为a || (b && c) a || (b && c) because && has higher precedence than that of || a || (b && c)因为&&优先级高于||优先级 operator. 操作员。 b and c binds to && . bc绑定到&&
As logical operators evaluates from left to right , first a is evaluated, if it is true then b && c would not be evaluated because || 逻辑运算符从左到右求值时 ,首先对a求值,如果为true则不会对b && c求值,因为|| operator performs short circuit evaluation of its operand. 操作员对其操作数进行短路评估。

If a evaluated as false then b && c will be evaluated, in which b is evaluated first and on being true c is evaluated, otherwise c would not be evaluated because of the same reason that && performs short circuit evaluation of its operand. 如果a评价为falseb && c将被评估,其中b是第一和上被评估true c被评估,否则c不会由于同样的原因评价为&&其操作数的执行短路评价。

I guess the program will first find b && c, let's call it d, and then find a || 我猜程序会先找到b && c,让我们称之为d,然后再找到||。 d, right? d,对吗?

No. 没有。

First of all, operator precedence only affects grouping of operators and operands; 首先,运算符优先级仅影响运算符和操作数的分组。 it does not affect the order in which expressions are evaluated. 不会影响表达式的计算顺序。 In general, evaluation order is left unspecified ; 通常,评估顺序不明确 for example, in an expression like a + b * c , each of a , b , and c may be evaluated in any order . 例如,在类似a + b * c的表达式中, abca都可以按任意顺序求值。 Similarly, b * c must be evaluated before a + b * c , but that doesn't mean that b * c must be evaluated before a . 同样, b * c必须在a + b * c之前进行求值,但这并不意味着b * c必须在a之前进行求值。

The || || and && operators are special in that they force left-to-right evaluation; &&运算符的特殊之处在于它们强制执行从左到右的评估; the left-hand side will be fully evaluated (and all side effects applied) first. 首先将完全评估左侧(并应用了所有副作用)。 Also, both operators use "short-circuit" evaluation; 另外,两个运营商都使用“短路”评估; if the value of the expression can be determined from the left-hand operand, then the right-hand operand won't be evaluated at all . 如果可以从左边的操作数来确定表达式的值,则右边的操作数将不被在所有评估。

Since && has higher precedence than || 由于&&优先级高于|| , the expression will be parsed as a || (b && c) ,该表达式将被解析为a || (b && c) a || (b && c) , meaning the result of a will be OR'd with the result of b && c . a || (b && c) ,表示的结果a将是或运算与的结果b && c However, since || 但是,由于|| and && force left to right evaluation, a will be evaluated first. &&强制从左到右评估,将首先评估a If the result of a is 0, then b will be evaluated. 如果结果a为0,则b将被评估。 If the result of b is non-zero, then c will be evaluated. 如果b的结果不为零,则将评估c If b is 0, then c won't be evaluated. 如果b为0,则不会评估c If a is non-zero, then neither b nor c will be evaluated. 如果a不为零,则bc都不会被求值。

Here's a table showing the various possibilities; 下表显示了各种可能性; NZ means "non-zero", and NE means "not evaluated": NZ表示“非零”,NE表示“未评估”:


a    b    c      b && c      a || b && c
-    -    -      ------      -----------
0    NZ   NZ     true  (1)   true  (1)
0    NZ   0      false (0)   false (0)
0    0    NE     false (0)   false (0)
NZ   NE   NE     NE          true  (1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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