会是什么x的计算顺序= X + + + + X;是? [英] What would the evaluation order of x = x++ + ++x; be?

查看:189
本文介绍了会是什么x的计算顺序= X + + + + X;是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/949433/could-anyone-explain-these-undefined-behaviors-i-i-i-i-i-etc\">Could谁能解释这些不确定的行为,(I = I + + + + I,I = I ++等&hellip;)

在Java的指定的评价为了被左到右。这是C和C ++的情况下,也还是依赖于实现?我记得,评价顺序是不确定的函数参数,而是分前pressions什么?

In Java the evaluation order is specified to be left-to-right. Is this the case for C and C++ as well, or is it implementation dependent? I do remember that the evaluation order is unspecified for function arguments, but what about sub-expressions?

推荐答案

这是不确定的,其中的参数 + 首先计算 - 但也不至于连事,因为在C和C ++,修改两次相同的对象,而无需插入顺序点是完全不确定的行为。

It is unspecified which of the arguments to + is evaluated first - but that doesn't even matter, because in C and C++, modifying the same object twice without an intervening sequence point is completely undefined behaviour.

在这里,您正在修改 X 的时间中间没有顺序点,所以你富裕的保留;)

Here you're modifying x three times without an intervening sequence point, so you're well off the reservation ;)

C99标准的相关部分是6.5防爆pressions

The relevant part of the C99 standard is "6.5 Expressions":

2的previous和下一间
  序列点的对象应具有
  其存储的值修改最多一次
  由前pression的评价。
  此外,前值应为
  只读确定该值是
  储存。

2 Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.

3算子的分组和
  操作数由表示
  句法。除指定后
  (对于函数调用(),放大器;&放;, ||,?,
  顿号运营商)的顺序
  SUBEX pressions和评价
  顺序的副作用发生
  都是不确定的。

3 The grouping of operators and operands is indicated by the syntax. Except as specified later (for the function-call (), &&, ||, ?:, and comma operators), the order of evaluation of subexpressions and the order in which side effects take place are both unspecified.

这是可能的代写法律code演示评估未指定的顺序 - 例如:


It's possible to write legal code that demonstrates the unspecified order of evaluation - for example:

#include <stdio.h>

int foo(void)
{
    puts("foo");
    return 1;
}

int bar(void)
{
    puts("bar");
    return 2;
}

int main()
{
    int x;

    x = foo() + bar();
    putchar('\n');

    return x;
}

(不管你得到的输出是不确定的 foobar的 barfoo )。

这篇关于会是什么x的计算顺序= X + + + + X;是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆