运算符优先级.. () 和 ++

发布于 2024-10-16 01:00:34 字数 717 浏览 5 评论 0原文

致敬..

我有一个不寻常的问题。 在此表中 在MSDN库中我们可以看到 () 的优先级高于 ++ (Pre-increment) 。 但是当我运行这段代码时,似乎 ++(prefex) 的优先级更高:

int main()
{
    int a=3,b=2,x;
    x=++a + (a-b);
    cout<<"x= "<<x;

    return 0;
}

答案是:

x=6

这种情况仅在 prefex ++ 中发生,并且按照我预期的后增量方式工作。

有什么理由吗? 问候..

int main()
{
    int a=3,b=2,x;
    x=a++ + (a-b);
    cout<<"x= "<<x;

    return 0;
}

x=4

(我使用 Microsoft Visual C++ 2010 Express)

Salute..

I have an unusual problem.
Here in this table in MSDN library we can see that precedence of () is higher than ++ (Pre-increment) .
but when I run this code, it seems that precedence of ++(prefex) is higher:

int main()
{
    int a=3,b=2,x;
    x=++a + (a-b);
    cout<<"x= "<<x;

    return 0;
}

and the answer is :

x=6

This happens with prefex ++ only and works as I expect with post-increment.

Is there any reason?
Regards..

int main()
{
    int a=3,b=2,x;
    x=a++ + (a-b);
    cout<<"x= "<<x;

    return 0;
}

x=4

(I use Microsoft Visual C++ 2010 express)

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

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

发布评论

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

评论(8

度的依靠╰つ 2024-10-23 01:00:34

像往常一样,这是未定义的行为+ 处没有 序列点,因此未定义++ 在什么时候更新 a。这不是一个优先级问题。

As usual, this is undefined behaviour. There is no sequence point at the +, so it is not defined at what point the ++ updates a. This is not a precedence issue.

迷荒 2024-10-23 01:00:34

这里有两个误解。

第一:表中,()指的是函数调用,而不是用于分组的括号。用于分组的括号不是具有特定优先级的运算符,而是一种强制执行与运算符优先级给出的解释不同的解释的方法。因此,无论运算符的优先级如何,括号中分组的任何内容都被视为一个单元。

第二:运算符优先级是指运算符在解析不明确的语法时所采用的优先顺序;它并不指副作用的时间顺序。因此,prefix ++ 总是在计算表达式之前增加值,posfix ++ 总是在计算表达式之后增加值,与语法运算符优先级无关。

There are two misunderstandings here.

The first: In the table, () refers to function calls, not to parentheses used for grouping. Parentheses used for grouping are not an operator with a certain precedence, but a means for enforcing a different interpretation than that given by operator precedence. Thus, whatever is grouped in parentheses is treated as a unit, no matter what the precedences of the operators are.

The second: Operator precedence refers to the order of precedence the operators take in parsing otherwise ambiguous syntax; it doesn't refer to the temporal order of side effects. Thus, prefix ++ always increases the value before the expression is evaluated, posfix ++ always increases the value after the expression is evaluated, independent of syntactic operator precedences.

掩于岁月 2024-10-23 01:00:34

这个 x=a++ + (ab); 是未定义的行为,无论它是 ++a 还是 a++

原因是:您使用两个参数调用 operator+a++(ab),但未定义这两个参数中的哪一个首先评价。


编辑: - 我发现您不明白这里的问题,我将添加更多信息:

operator++(后缀或前缀,无论什么),更改 变量的值,因此这使得该运算符更加特殊,即 operator+operator- 等。因此,如 ++a< /code> 和 a++ 更改了 a 的值,在再次使用 a 之前,您需要在此之后有一个序列点。 operator+ 不是序列点,因此就像您调用了 operator+( ++a, (ab) ); 一样。该标准没有提及参数评估的顺序,因此这给我们带来了未定义的行为

更好的?

This x=a++ + (a-b); is undefined behavior, no matter if it is ++a or a++.

The reason is: you're calling operator+ with two arguments: a++ and (a-b), but it's undefined which of the two arguments will be evaluated first.


EDIT: - as I see you don't understand the problem here, I'll add some more info:

operator++ (postfix or prefix, whatever), changes the value of the variable, so this makes this operator more special, that operator+, operator-, etc. So, as ++a and a++ changes the value of a, you need to have a sequence point after this, before using a again. operator+ is NOT a sequence point, so it's like you've called operator+( ++a, (a-b) );. The standard says NOTHING about the order of evaluation of the parameters, so this brings us the undefined behavior.

Better?

老娘不死你永远是小三 2024-10-23 01:00:34

优先级并不决定评估的顺序。优先级指定运算符如何绑定到操作数;不是运算符的评估顺序。

优先级表源自语法,它们不能替代语法。

另外,您不应该假设 JScript 优先级表必然对 C++ 代码有任何影响。

Precedence does not determine order of evaluation. Precedence specifies how operators bind to operands; not the order in which the operators are evaluated.

Precedence tables are derived from the grammar, they are not a replacement for it.

Also, you shouldn't assume that a JScript precedence table necessarily has any bearing on C++ code.

肩上的翅膀 2024-10-23 01:00:34

这是调用未定义的行为。通过前增量或后增量修改 a 与在括号表达式中使用它之间没有顺序点。这实际上与运算符优先级无关。考虑一个更简单的例子:

int a = 1;
int b = (a=2) + a;

b 的值应该是多少?编译器可以以任何顺序计算 + 的左侧和右侧,因为一般来说,两个顺序都会得到相同的答案,除非您修改了其中一个变量表达式并再次引用它而不插入序列点。正如我所说,这是未定义的行为,一个编译器可能会给出 4 个,另一个编译器可能会给出 3 个,第三个可能会导致你的计算机着火。

This is invoking undefined behavior. There is no sequence point between the modification of a via pre-increment or post-increment and its use in the parenthetical expression. This actually has nothing to do with operator precedence. Consider a simpler example:

int a = 1;
int b = (a=2) + a;

What should the value of b be? The compiler is allowed to evaluate the left hand and right hand sides of + in any order, because in general you'll get the same answer for both orders, except when you modify one of the variables in the expression and refer to it again with no intervening sequence point. This is, as I said, undefined behavior, one compiler might give 4, another 3, a third might light your computer on fire.

笑叹一世浮沉 2024-10-23 01:00:34

我只给你一个链接:

序列点

由 Prasoon Saurav 在 StackOverflow.com 上解释

I will just give you a link:

Sequence Points

explained on StackOverflow.com by Prasoon Saurav

傲影 2024-10-23 01:00:34

据我了解,每当您在公式中使用类似 a++ 的内容时,都会使用“a”值的副本进行数学计算,因为“++”仅在其他操作之后才起作用,因此公式永远不会更新。但我对此可能是错的。

所以如果你有 x=++a + b,其中 a = 1 且 b = 2
你会得到像 x = 1 + 2 这样的东西,并且通过 x = a++ + b,你会得到 x = 2 + 2,通过值替换。

It is my understanding that when-ever you use something like a++ in a formular, a copy of value of "a" is used for the math, since "++" only works after other operations, the formula is never updated. but I may be wrong about this.

so if you have x=++a + b, where you a = 1 and b = 2
you get something like x = 1 + 2, and with x = a++ + b, you get x = 2 + 2, on the values substitution.

赏烟花じ飞满天 2024-10-23 01:00:34

它不存在任何优先级问题。

如果你能解决这个问题,那么只要我们有 = 运算符,它就会从“从右到左”解决。意味着右侧表达式将被求解,并且 ans 将被分配给变量x。在解决右侧表达式时,我们有 + 运算符,它使用“从左到右”的方法来解决。所以++a会先求解,然后根据规则括号求解。所以 ++a 将生成 4 ,而 (ab) 将给出 2 所以最终的加法将给出结果 6

It does not have any precedence issue.

If you will solve the question then whenever we have = operator it will be solved from "right to left". Means right side expression will be solved and ans will be assigned to variable x. While solving right side expression we have + operator which uses "left to right" approach for solution. So ++a will be solved first and then according to rule bracket will be solved. So ++a will generate 4 and (a-b) will give 2 so final addition will give result 6.

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