以下问题的输出是什么? [英] What would be the output of the following problem?

查看:80
本文介绍了以下问题的输出是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int main()


{
	int a,b;
	a=5;
	
	b=++a + a++;
	
	cout<<b;

	
	
	
}





我有什么试过:





What I have tried:

引用:

为什么不来12而不是13?

why does not it come 12 instead of 13?

推荐答案

取决于。这取决于你使用的编译器,编译器的版本,风的方式,...

增量前和后增量运算符被定义为增加前后的值。使用了值 - 但C或C ++规范中没有任何内容表明应该何时发生。它可能在行执行之前,如果完成则可以在行之后。它甚至可以立即使用该命令。更糟糕的是,表达式中的执行顺序也没有定义(除了涉及&&和||运算符的特定情况) - 编译器可以自由地将其写入从右到左,从左到右,或从上到下工作或者自下而上,或者......你明白了。

优化可能会改变所有这些,因此你的生产编译代码可能与开发代码的工作方式不同。



所以不要做那样的事情:结果不能保证也不能依赖 - 仅仅因为你 可以 做什么,并不意味着你 应该
Depends. It depends on the compiler you use, the version of the compiler, which way the wind is blowing, ...
The pre- and post-increment operators are defined to increase the value before or after the value is used - but nothing in the C or C++ specification says when that should occur. It could be before the line is executed at all, it could be after the line if finished with. It could even be immediately the command is used. To make things worse, the execution order within an expression is not defined either (except in specific cases involving && and || operators) - the compiler write it free to work right to left, or left to right, or top down, or bottom up, or ... you get the idea.
And optimisations may change all that, so your production compiled code may not work the same as your development code did.

So don't do things like that: the results are not guaranteed and cannot be relied upon - just because you can do something, does not mean you should!


深呼吸并阅读 c ++ - 未定义的行为和序列点 - 堆栈溢出 [ ^ ]。


这篇关于以下问题的输出是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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