简体   繁体   English

C,*和/以及%运算符优先级如何工作?

[英]How does operator precedence grouping work in C for *, /, and %?

Referring to the O'Reilly pocket reference for C, I'm a little confused by the description for grouping of the * , / , and % operators. 关于C的O'Reilly袖珍参考,对于*/%运算符的分组的描述,我有些困惑。 The book says that grouping occurs left to right -- now I think I'm getting grouping confused with evaluation order. 这本书说分组从左到右进行-现在我想将分组与评估顺序相混淆。 Given the following equation, and the rules established from the book, I would have thought that... 鉴于以下等式,以及从书中建立的规则,我本以为...

int x = 4 / 3 * -3

... evaluates to 0 , because... ...的值为0 ,因为...

1: 4 / 3 * -3
2: 4 / -9
3: 0

... however, it actually evaluates to -3 , and it seems to use this method... ...但是,实际上它的计算结果为-3 ,并且似乎使用了此方法...

1: 4 / 3 * -3
2: 1 * -3
3: -3

Why is that? 这是为什么?

It makes sense to me: 对于我,这说得通:

int x = 4 / 3 * -3;

Grouping left to right, we get: 从左到右分组,我们得到:

int x = (4 / 3) * -3
int x = ((4 / 3) * -3);

Also see the precedence table . 另请参见优先级表 They are at the same precedence, so they bind left to right. 它们具有相同的优先级,因此它们从左到右绑定。

You need to know both the precedence and the associativity of operators. 您需要知道运算符的优先级关联性。

Multiplication (*) has higher precedence than addition (+), which is why 2+3*4 is interpreted as 2+(3*4), both in C and normal math. 乘法(*)的优先级高于加法(+),这就是为什么在C语言和普通数学中2 + 3 * 4都被解释为2+(3 * 4)的原因。 But in an expression like 2*3/4, or 2*3*4, the operators all have the same precedence, and you need to look at the associativity. 但是在2 * 3/4或2 * 3 * 4之类的表达式中,运算符的优先级都相同,因此您需要查看关联性。 For most operators, it is left to right, which means that you start grouping from the left: 2*3/4 becomes (2*3)/4, 2*3*4*5 becomes ((2*3)*4)*5, and so on. 对于大多数运算符,它从左到右,这意味着您从左开始分组:2 * 3/4变成(2 * 3)/ 4,2 * 3 * 4 * 5变成((2 * 3)* 4 )* 5,依此类推。

An exception is assignment, which is an operator in C. Assignment is right associative, so a=b=3 should be read as a=(b=3). 例外是赋值,它是C中的运算符。赋值是右关联的,因此a = b = 3应该读作a =(b = 3)。

Any good C book or tutorial should have a table of all the operators (such as this one ), with both precedence and associativity. 任何好的C书籍或教程都应该有一张所有运算符的表(例如this ),具有优先级和关联性。

Visit the following URL. 访问以下URL。 It is very useful all the topics in C. So you can use the operator precedence also. 它对C语言中的所有主题都非常有用。因此,您也可以使用运算符优先级。

 http://www.goldfish.org/books/The%20C%20Programming%20Language%20-%20K&R/chapter2.html#s2.12

Here , it is left associative for system recognisation . 在这里,它与系统识别相关联。 So , it will execute second example only for evaluating the expression. 因此,它将仅执行第二个示例以评估表达式。

IMHO, it is good to know about these operator precedences, but it is better to use parentheses whenever there is doubt :-). 恕我直言,最好知道这些运算符的优先级,但是最好在有疑问时使用括号:-)。 As the masters say, the code is more for human readers than for the machine; 就像大师所说的那样,对于人类读者而言,代码比对机器而言更多。 if the author is not sure, nor will be the readers. 如果作者不确定,读者也不会。

这些 链接应该可以帮助您。

乘法和除法保持关联,因此发生的是第二顺序-运算分组为(4/3),然后将结果乘以-3。

For math, C works just like you learned in high scool. 对于数学,C的工作原理就像您在高学中学习的一样。 Remember BODMAS (Brackets of Division, multiplication, addition and subtraction). 记住BODMAS(除法,乘法,加法和减法括号)。 This means it looks for a calculation from the left to the right. 这意味着它将从左到右寻找一​​个计算。 In this case it sees 4/3 and calculates the answer, then multiplies the answer with -3. 在这种情况下,它将看到4/3并计算答案,然后将答案乘以-3。 You can use brackets to fix that ( 4/(3*-3) ). 您可以使用方括号将其修复( 4/(3*-3) )。 Have a look at this page for a summary of how C orders operators and performs the calculations. 请查看此页面 ,以摘要了解C如何对运算符进行排序并执行计算。

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

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