运算符优先级..()和++ [英] Operator Precedence.. () and ++
问题描述
致敬..
我有一个不寻常的问题。
在这里在表MSDN库中我们可以看到,()的优先级高于 ++(预增量)。
,但是当我运行这个代码,似乎 ++(prefex)的优先级更高:
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;
}
,答案是:
x = 6
x=6
这是发生在prefex ++, post-increment 。
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
x=4
(我使用Microsoft Visual C ++ 2010 express)
(I use Microsoft Visual C++ 2010 express)
推荐答案
像往常一样,这是未定义的行为。在 +
上没有序列点, ,所以它没有定义在 ++
更新 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.
这篇关于运算符优先级..()和++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!