C 中的关联性和序列点

发布于 2024-12-19 07:02:35 字数 334 浏览 4 评论 0原文

由于 '?' 的结合性从右到左,任意2个连续的'?'运营商一定要这样对待,对吧?

现在,

int x=-1;
int y=x?x++?x:-1:1;

我希望它的执行方式为:

int y = x ? (x++?x:-1) : 1;

现在,由于它是从右到左执行的,所以当遇到第一个“?”时在语句中,x 的值为 0,表达式如下,

int y= x? 0 : 1;

因此我期望 y 为 1,但它在我的 dev-cpp 上显示为零。我哪里错了?

Since the associativity of '?' is from right to left,any 2 consecutive '?' operators must be treated as such,Right?

Now,

int x=-1;
int y=x?x++?x:-1:1;

I expect this to be executed as:

int y = x ? (x++?x:-1) : 1;

Now since its being executed from right to left,when encountering the first '?' in the statement,x's value is 0 and the expression is as

int y= x? 0 : 1;

hence i expected y to be 1,but it shows Zero on my dev-cpp.Where am i wrong?

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

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

发布评论

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

评论(3

戏舞 2024-12-26 07:02:35

您的评估顺序错误。在 中? b : c, a 始终首先计算,然后计算 bc

我已经标记了您的示例,以便我可以识别子表达式:

            c
int y=x?x++?x:-1:1;
      a bbbbbbbb

(a) 被求值,产生 -1,因此 (b) 被求值。在那里,x++ 被求值,再次产生 -1,因此 (c) 被求值。此时,x 为 0。

或者,使用更详细、更清晰的代码,就好像您说:

int x = -1;
int y;
if (x != 0)
{
    int new_x = x + 1;
    if (x != 0)
    {
        y = new_x;
    }
    else
    {
        y = -1;
    }
}
else
{
    y = 1;
}

You have the order of evaluation wrong. In a ? b : c, a is always evaluated first, then either b or c is evaluated.

I've marked up your example so that I can identify subexpressions:

            c
int y=x?x++?x:-1:1;
      a bbbbbbbb

(a) is evaluated, yielding -1, so (b) is evaluated. There, x++ is evaluated, yielding -1 again, so (c) is evaluated. At this point, x is 0.

Or, with more verbose, clearer code, it's as if you said:

int x = -1;
int y;
if (x != 0)
{
    int new_x = x + 1;
    if (x != 0)
    {
        y = new_x;
    }
    else
    {
        y = -1;
    }
}
else
{
    y = 1;
}
不打扰别人 2024-12-26 07:02:35

操作:

Assign y to value = 
    if(x): --> x = -1, so true as it is non-zero
    {
      if(x): --> x = -1 ,so true as x will increment later due to post increment
       x= x +1; --> increment x, so x = 0 . This is the value assigned. So y = 0;
     else:
       -1
    }
    else: 
    {
      1
    }

希望这有帮助!

Operations:

Assign y to value = 
    if(x): --> x = -1, so true as it is non-zero
    {
      if(x): --> x = -1 ,so true as x will increment later due to post increment
       x= x +1; --> increment x, so x = 0 . This is the value assigned. So y = 0;
     else:
       -1
    }
    else: 
    {
      1
    }

Hope this helps!

你曾走过我的故事 2024-12-26 07:02:35

你的问题的答案是在 C/C++ int y = x ? (x++?x:-1) : 1; 我们将在 ? 处命中两个序列点。对序列点中的变量进行的任何更新操作将在该序列结束后生效。让我们看看我们手头的例子。

第一个序列点是左起第一个 ?

x=-1; (Actual Value)
x=-1; (Value used in expression)
y=-1?(x++?x:-1):1;

第二个序列点是左起第二个 ?。如上所述,更新操作在序列之后有效,因此即使存在 x++ ,该序列中使用的值也是 -1 并且更新后的值将在下面使用。

x=0; (Actual Value, bcoz of x++)
x=-1; (Value used in expression)
y=-1?x:-1;

现在希望

x=0; (Actual Value)
x=0; (Value used in expression)
y=x;
y=0;

这现在有意义。

The answer to your question is that in C/C++ int y = x ? (x++?x:-1) : 1; we will hit two sequence points at ?. Any update operations to variable with in a sequence point will be effective after that sequence is over. So lets look at our example in hand.

First sequence point is first ? from left.

x=-1; (Actual Value)
x=-1; (Value used in expression)
y=-1?(x++?x:-1):1;

Second sequence point is second ? from left. As mentioned above the update operations are effective after sequence so even though x++ is there the value used in this sequence is -1 and updated value will be used in following.

x=0; (Actual Value, bcoz of x++)
x=-1; (Value used in expression)
y=-1?x:-1;

Now it will be

x=0; (Actual Value)
x=0; (Value used in expression)
y=x;
y=0;

Hope this make sense now.

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