运算符优先级问题

发布于 2024-12-01 19:29:46 字数 202 浏览 2 评论 0原文

O/p 结果为 x=2,y=1,z=1,这与运算符优先级不一致。我在 Turbo C++ 编译器上运行这个:

void main()
{
    int x,y,z,q;
    x=y=z=1;
    q=++x || ++y && ++z;
    printf("x=%d y=%d z=%d",x,y,z);
}

The O/p comes out to be x=2,y=1,z=1 which doesnt agree with the operator precedence. I was running this on Turbo c++ compiler:

void main()
{
    int x,y,z,q;
    x=y=z=1;
    q=++x || ++y && ++z;
    printf("x=%d y=%d z=%d",x,y,z);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

み零 2024-12-08 19:29:46

实际上结果完全符合标准 C。逻辑或运算符 (||) 在 ++x 之后短路,因为它的计算结果为非零数,因此其余的都被忽略。

所以从x=1y=1z=1开始,短路后,你得到x=2< /code>、y=1z=1

Actually the result is in complete accordance with standard C. The logical or operator (||) short circuits after ++x because it evaluates to a non-zero number, so the rest of them are ignored.

So starting at x=1, y=1, z=1, after the short circuit, you get x=2, y=1, z=1.

拧巴小姐 2024-12-08 19:29:46

运算符优先级无论如何都不会决定运算符的执行顺序。运算符优先级仅定义运算符及其操作数之间的分组。在您的情况下,运算符优先级表示表达式

q = ++x || ++y && ++z

被分组为

q = ((++x) || ((++y) && (++z)))

其余部分与运算符优先级完全无关。

其余的由每个特定运算符的语义决定。本例中的顶级运算符是 |||| 运算符的特定属性是它始终首先计算其左侧。如果左侧尺寸结果非零,那么它甚至不会尝试评估右侧。

这正是您的情况所发生的情况。左侧是++x,其计算结果为非零值。这意味着具有给定初始值的整个表达式在功能上等同于仅仅

q = (++x != 0)

|| 运算符的右侧甚至没有被触及。

Operator precedence does not in any way determine the order in which the operators are executed. Operator precedence only defines the grouping between operators and their operands. In your case, operator precedence says that the expression

q = ++x || ++y && ++z

is grouped as

q = ((++x) || ((++y) && (++z)))

The rest has absolutely nothing to do with operator precedence at all.

The rest is determined by the semantics of each specific operator. The top-level operator in this case is ||. The specific property of || operator is that it always evaluates its left-hand side first. And if the left-hand size turns out to be non-zero, then it does not even attempt to evaluate the right-hand side.

This is exactly what happens in your case. The left-hand side is ++x and it evaluates to a non-zero value. This means that your whole expression with the given initial values is functionally equivalent to a mere

q = (++x != 0)

The right-hand side of || operator is not even touched.

作业与我同在 2024-12-08 19:29:46
x=y=z=1;

使所有变量 = 1

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

由于 ++x 使其 = 2,并且由于它不为零,因此它停止检查其他条件,因为第一个条件为 true

因此,x=2,并且 y 和 z = 1

x=y=z=1;

Makes all the variables = 1

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

Since ++x makes it = 2 and since it is not zero it stops checking the other conditions because the first one is true.

Thus, x=2, and y and z = 1

鹿港巷口少年归 2024-12-08 19:29:46

逻辑 && (AND) 和 || (OR) 运算符会发生短路。

“逻辑运算符保证从左到右评估其操作数。但是,它们评估确定表达式结果所需的最少数量的操作数。这称为“短路”评估。”

因此,对于逻辑运算符,始终按从左到右的顺序计算(无论 ||&&)。
正如前面提到的,这里的优先级仅决定谁抢谁。
然后从左到右规则;

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

//ok, lets play by rule, lets see who takes who:
//first pass ++ is badass here (has highest precedence)
//q = (++x) || (++y) && (++z)

//second pass &&'s turn
//q = (++x) || ((++y) && (++z))

//done, let's do left to right evaluation
q = (++x) || rest..
q = (true)|| whatever..

希望能帮助更清楚。

Logical && (AND) and || (OR) operators are subject to Short-Circuit.

"Logical operators guarantee evaluation of their operands from left to right. However, they evaluate the smallest number of operands needed to determine the result of the expression. This is called "short-circuit" evaluation."

Thus, for logical operators always evaluated as (no matter || or &&) left to right.
And as previously mentioned, precedence here only determines who takes who.
Then left to right rule;

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

//ok, lets play by rule, lets see who takes who:
//first pass ++ is badass here (has highest precedence)
//q = (++x) || (++y) && (++z)

//second pass &&'s turn
//q = (++x) || ((++y) && (++z))

//done, let's do left to right evaluation
q = (++x) || rest..
q = (true)|| whatever..

hope that helps more clear.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文