简体   繁体   English

C中的运算符优先级

[英]operators precedence in C

have a trouble understanding this expression: 理解这个表达式有困难:

(x + y - 1) / y * y

The operators precedence is as follows (as per my understanding and K&R2, 2.12 table 2.1): 运算符优先级如下(根据我的理解和K&R2,2.12表2.1):

1) evaluate what is in the parens: firstly (x+y), then (x+y) -1 1)评估parens中的内容:首先是(x + y),然后是(x + y)-1

2) '*' operator has higher priority then '/' so it's supposed to go first, bit it appears that (y * y) is evaluated and then the result of (x+y-1) is divided by the product (y*y). 2)'*'运算符的优先级高于'/'所以它应该先行,比特看起来(y * y)被评估,然后(x + y-1)的结果除以乘积(y) * Y)。 I don't quite get it. 我不太清楚。

3) I ever heard that normally rounding up iw written in this form: 3)我听说过通常以这种形式写的:

(x + y - 1) / y * y

Is that correct? 那是对的吗? Thank you very much in advance! 非常感谢你提前!

Unary operator * has higher precedence than / , but that is for pointer dereferencing. 一元运算符*优先级高于/ ,但这是指针解除引用。

Multiplication * and division / have the same left-to-right precedence. 乘法*和除法/具有相同的从左到右的优先级。

no, * and / have same precedence. 不, */具有相同的优先权。 (it is called " precedence ", not "priority") (它被称为“ 优先 ”,而不是“优先”)

(x + y - 1) / y * y

would be 将会

( ( (x+y) - 1 ) / y ) * y

The operation tree would be: 操作树将是:

           *
          / \
         /   y
        ÷
       / \
      /   y
     -
    / \
   /   1
  +
 / \
x   y

As mentioned earlier the "*" and "/" have the same precedence so they are evaluated left to right. 如前所述,“*”和“/”具有相同的优先级,因此它们从左到右进行评估。 Totally disambiguating the expression gives: 完全消除表达式的歧义给出了:

( ( ( (x + y) - 1) / y) * y)

1) right 1)对

2) No, "/" and "*" is the same priority. 2)否,“/”和“*”具有相同的优先级。 So it will be performed from left to right. 所以它将从左到右执行。

3) I don't understand what your "round up" mean. 3)我不明白你的“围捕”是什么意思。 But for example: 但是例如:

With y = 2, x = 2 y = 2,x = 2

(x + y -1) / y * y = (2 + 2 - 1) / 2 * 2 = (3 / 2) * 2 = 1 * 2 = 2 (x + y -1)/ y * y =(2 + 2 - 1)/ 2 * 2 =(3/2)* 2 = 1 * 2 = 2

3 / 2 = 1 because this is the division of integer. 3/2 = 1,因为这是整数的除法。

'*' operator has higher priority then '/' '*'运算符的优先级高于'/'

Incorrect. 不正确。 Both * and / have equal precedence and are left-associative. */具有相同的优先级并且是左关联的。

After evaluating the parenthesis expression we have: 在评估括号表达式后,我们有:

(x + y - 1) / y * y = z / y * y      // where z = x + y -1

                    = (z / y) * y    // because of associativity.

I may be wrong, but as far as I recall / and * have same precedence. 我可能错了,但据我记得/和*有相同的优先权。 So the result should be (x+y+1). 所以结果应该是(x + y + 1)。

Maybe you should say (x+y+1)/(y*y) if that's what you want the result to be. 也许你应该说(x + y + 1)/(y * y),如果这就是你想要的结果。

It never costs much to disambiguate in any case. 在任何情况下消除歧义都不会花费太多。

As to using ((x+y-1)/y)*y to round up or down as the case may be, this is a dubious and not very portable practice. 至于根据具体情况使用((x + y-1)/ y)* y向上或向下舍入,这是一个可疑且不太便携的做法。 Several reasons: 1/ you must know or remember that y is some sort of int, 2/ x must also be some sort of int, 3/ results may vary depending on the compiler. 有几个原因:1 /你必须知道或记住y是某种int,2 / x也必须是某种int,3 /结果可能因编译器而异。

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

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