Increment/ decrement doubt

I was just playing with the increments etc. But I am unable to find the reason of the output.

1
2
3
4
5
6
7
8
9
10

#include<iostream>
using namespace std;
int main(){
int i=1;

int a=  ++i + ++i;
cout<<a;
return 0;
}


Here, the console displays me 6.
But according to me, it should be 5.
Can anyone , explain me the reason?


Thanks in advance :)
Last edited on
The expression ++i + ++i is an error in C++ (or C, for that matter), the compilers are allowed to do anything at all. It's like reading from uninitialized memory.

see http://en.cppreference.com/w/cpp/language/eval_order for some details why
As you did not initialize variable i then I do not understand why you think that a must be equal to 6.:)
Sorry, I directly, typed it in the forum.
'i' is an integer with value 1.

I ran the program, in codeblocks.
Last edited on
@abeginner23235616

'i' is an integer with value 1.


And why do a have to be equal to 6?
cubbi is correct.

You literally could get any output here. Trying to find a reason for your output is pointless.
Actually, I have run it on 2-3 compilers..
dev c++

compileronline.com

turbo c++

codeblocks

All of them, have given 6 as an output.
P.S: This questions have been asked in my exam, that is why I am resisting to know the reason of the output.

Anyways thanks for your concern
I have run it on 2-3 compilers..

Running an undefined program is quite pointless, but in case it would help you understand the scope of the problem, I'll run it on a few more compilers for you:
linux/gcc-4.8.1: 6
linux/clang-3.3: 5
linux/intel-13.1.1: 5
ibm/xlc-11.1: 5
ibm/gcc-4.7.2: 6
sparc/sun-12: 6

the compilers that print 5 and the compilers that print 6 are equally correct
Last edited on
Okay, thanks for your concern.
Ill ask my teacher then.

But can you explain me, how 6 is right?

It is understandable for 5.
But I cant find any reason for 6 :/
Last edited on
@abeginner23235616

Okay, thanks for your concern.
Ill ask my teacher then.


... and the teacher will ask the forum.:)
Expression ++i is lvalue. So the compiler have to write the increased value to i.

So we have

++i + ++i;

The order of evaluation of operands is not important.

So the compiler increases i and writes the new value in the cell occupied by i. i will be equal to 2. Then again the compiler reads i increases it and writes the new value in the cell occupied by i. i will be equal to 3. Then it adds the new value of i to itself and gets 6.
Last edited on
@Cubbi demonstrated that some compilers satisfy the new C++ 11 standard and others not. Compilers that satisfy the new standard return 6. Compilers that does not satisfy the new standard return 5.:)
Just in case anyone takes the above post seriously despite the emoticon, C++11 and C++03, although differently phrased, have effectively identical rules as far as this expression is concerned: it's undefined.

abeginner23235616 wrote:
But can you explain me, how 6 is right?
Everything is right when executing such code. 5. 6. 42. crash. everything. The compiler operates under the assumption that it will never encounter such code (except for the front-end error checking: gcc and clang for example, report this as a warning)
Last edited on
@Cubbi


There are only your emotions. The question is not simple as you are trying to represent it.
My point of view is that these two increments are indeterminately sequenced and it is not important which expression will be evaluated first.
vlad wrote:
There are only your emotions.

Whether the program is defined or not is not related to anyone's emotions.

vlad wrote:
My point of view is that these two increments are indeterminately sequenced and it is not important which expression will be evaluated first.

And the point of view of the C++ standard is that the two increments are unsequenced (using C++11 wording), which is why evaluation of the expression ++i + ++i where i has the type int is undefined behavior.
Last edited on
@Cubbi
And the point of view of the C++ standard is that the two increments are unsequenced (using C++11 wording), which is why evaluation of the expression ++i + ++i where i has the type int is undefined behavior.



I am not sure that it is the point of view of the C++ Standard and not just your point.
Topic archived. No new replies allowed.