int i = 2; printf("%d%d",++ i,++ i);如何以任何评估顺序进行操作 [英] int i=2; printf("%d %d",++i,++i); how can o/p be 4 4 with any order of evaluation

查看:169
本文介绍了int i = 2; printf("%d%d",++ i,++ i);如何以任何评估顺序进行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个表达式有未定义的o / p但是怎么能给出4 4个pls表明产生4 4的任何可能的机制

this expression has undefined o/p but how could it give 4 4 pls suggest any possible mechanism by which 4 4 is produced

推荐答案

它是未定义的行为,任何事情都可能发生。


然而,对于post和pre递增和递减运算符,不提供关于何时完成递增或递减的保证。只讨论运算符表达式使用的值。


即++我保证该值将是通过向i添加1而产生的值,但不保证何时准确我将被分配这个值。


在你的表达式中,编译器可以在将任何东西放入函数调用的堆栈之前非常简单地评估两个++运算符。但正如你所说的那样,这个操作是未定义的,猜测是没有意义的。
It''s undefined behaviour, anything can happen.

However with the post and pre increment and decrement operators no guarantee is provided about when the increment or decrement is done. Only about what value will be used for the expression of the operator.

i.e. ++i guarantees that the value will be the value produced by adding 1 to i but does not guarantee when exactly i will be assigned this value.


In your expression the compiler could have quite simply evaluated both ++ operators before putting anything on the stack for the function call. But as you an I have said this operation is undefined speculation is pointless.


根据我的观点...... o / p应该是3和4 ...为什么你得到4 4
as per my view..the o/p should be 3 and 4...why u getting 4 4


再次阅读Banfa的帖子。


在语句中多次更改同一个变量的值时,结果是不确定的。


看到3和4的显示需要一个假设。也许有一个编译器同意你,然后,也许不是。
Read Banfa''s post again.

When you change the value of the same variable more than once in a statement, the results are indeterminate.

Seeing a display of 3 and 4 requires an assumption. Maybe there''s a compiler that agrees with you, and then, maybe not.


这篇关于int i = 2; printf("%d%d",++ i,++ i);如何以任何评估顺序进行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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