问题描述
问候!
我想知道以下一段代码中的表达式如何评价以及为什么:
#include< iostream>
使用命名空间std;
int main()
{
int n = 5,p;
n = n + n ++;
cout<< n = << n<< endl;
n = 5;
p = n + n ++;
cout<< p = << p
返回0;
}
我用g ++和bcc32编译了它,结果是相同的:
n = 11
p = 10
第一个结果似乎很明显。但我不知道对于p变量的评估是什么。
。我真的很感兴趣它是如何工作的。提前谢谢,
Karlo。
Greetings!
I''m wondering how do the expressions in the following piece of code
evaluate and why:
#include <iostream>
using namespace std;
int main()
{
int n = 5, p;
n = n + n++;
cout << "n = " << n << endl;
n = 5;
p = n + n++;
cout << "p = " << p << endl;
return 0;
}
I compiled it both with g++ and bcc32 and the results are identical:
n = 11
p = 10
The first result seems obvious. But I don''t know what''s going on with
the evaluation of the p variable. I am really interested on how it
works. Thank you in advance,
Karlo.
推荐答案
没有办法预测或解释它是如何工作的。两个表达式
导致未定义的行为。
-
祝你好运,
Andrey Tarasevich
There''s no way to predict or explain how it will work. Both expressions
result in undefined behavior.
--
Best regards,
Andrey Tarasevich
没有正确的答案。编译器可以做任何想做的事情。
你的程序产生了不确定的行为,因为没有,所以考虑一个'正确的解决方案'是没有意义的。
-
Karl Heinz Buchegger
这篇关于不寻常的操作员行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!