Output of ++i + ++i + i + --i?

What will be the output of ++i + ++i + i + --i when i=5?
Compiler says output is 27. How?

Showing Answers 1 - 12 of 12 Answers

The behavior of this expression is *undefined*. Any expression that tries to change the value of an object more than once (or tries to change it and then use it in a value computation) without an intervening sequence point invokes undefined behavior. All expressions of the forms
x++ * x++
x = x++
a[i] = i++
a[i++] = i
invoke undefined behavior, and the results of such expressions will not be consistent or repeatable across different compilers or even different programs using the same compiler.
The order in which each subexpression is evaluated and the order in which each side effect is applied is left *unspecified* by the language definition, so the result can vary between different compilers, or the same compiler with different optimization settings, or even differences in the surrounding code. 27 is just one of many possible outputs, all of which are equally correct as far as the language definition is concerned.
Dont ever write code like that.

  Was this answer useful?  Yes

nitin

  • Sep 10th, 2018
 

114

  Was this answer useful?  Yes

shailesh

  • Nov 23rd, 2018
 

7+7+7+6=27.

  Was this answer useful?  Yes

Pujitha

  • Jun 27th, 2019
 

6+6+6+6=24

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions