简体   繁体   English

C 运算符中的优先级 == 和 ( = )

[英]Precedence in C operators == and ( = )

I have to analyze what some code in C does, and I have a doubt about what happens in a certain line.我必须分析 C 中的一些代码做了什么,我对某行发生的事情有疑问。 The code is代码是

#define PRINTX printf("%d\n", x)

void problem() { 
    int x = 2, y, z;
    x *= 3 + 2; PRINTX;
    x *= y = z = 4; PRINTX;
    x = y == z; PRINTX;
    x == (y = z); PRINTX; // This line is the problem
} 

This code snippet prints the resulting numbers:此代码段打印结果数字:

10
40
1
1  // This result **

the problem is that I'm still trying to figure out why does the last line prints out x = 1 , when the operation is x == (y = z) .问题是我仍在试图弄清楚为什么当操作是x == (y = z)时最后一行打印出x = 1 I'm having trouble finding out what that 1 means and the precedence of the operations.我很难找出 1 的含义以及操作的优先级。 Hope someone can help me: :)希望可以有人帮帮我: :)

Nothing in the last statement changes the value of x , so its value remains unchanged.最后一条语句没有改变x的值,所以它的值保持不变。


Parens were used to override precedence, forcing the = to be the operand of the == . Parens 用于覆盖优先级,强制=成为==的操作数。

An operator's operands must necessarily be evaluated before the operator itself, so we know the following:运算符的操作数必须在运算符本身之前计算,因此我们知道以下内容:

  • y is evaluated at some point before the = . y=之前的某个时间点被评估。
  • z is evaluated at some point before the = . z=之前的某个时间点进行评估。
  • x is evaluated at some point before the == . x==之前的某个时间点进行评估。
  • = is evaluated at some point before == . ===之前的某个时间点被评估。

That's it.而已。 All of these are valid orders:所有这些都是有效的订单:

  • z y = x == z y = x ==
  • y z = x == y z x == =
  • x y z = == x z == y =
  • etc.等等

But whenever x , y and z are evaluated, we can count on the following happening:但是每当xyz被评估时,我们可以指望以下发生:

  1. = assigns the value of z (currently 4 ) to y and returns it. =z的值(当前为4 )分配给y并返回它。
  2. == compares the value of x (currently 1 ) with the value returned by = ( 4 ). ==x的值(当前为1 )与= ( 4 ) 返回的值进行比较。 Since they're different, == returns 0 (which isn't used by anything).由于它们不同, ==返回0 (不被任何东西使用)。

As you see, nothing changed x , so it still has the value it previously had ( 1 ).如您所见, x没有任何变化,因此它仍然具有以前的值 ( 1 )。

In the last statement, nothing is changing the value of x.在最后一条语句中,没有改变 x 的值。 We are testing if x equals something, but we aren't changing it's value.我们正在测试 x 是否等于某个值,但我们并没有改变它的值。

So it continues having the same value as it had in the previous statement, in particular, a value of 1.所以它继续具有与前一个语句中相同的值,特别是值 1。

the reason is because the == operator checks if the 2 numbers are equal, and returns 1 if equal and 0 if not equal that is why it returns one you can check by making x= 1 and y=2 and using the == operator between them原因是因为 == 运算符检查 2 个数字是否相等,如果相等则返回 1,如果不相等则返回 0 这就是为什么它返回 1 您可以通过使 x= 1 和 y=2 并使用 == 运算符来检查它们之间

The comparison result of x and assignment of y with (y = z) is discarded. x的比较结果和y(y = z)的赋值被丢弃。 Last line could have dropped the compare: y = z; PRINTX;最后一行可能会放弃比较: y = z; PRINTX; y = z; PRINTX; . .

The assignment is not subsequently used either, so the line could have been PRINTX;该分配随后也未使用,因此该行可能是PRINTX; . .

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

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