+ 4

What will be the result of I=3; I=++I + I++; cout<<i

I thought it would be 7 but apparently not but why?

9th Sep 2016, 3:07 PM
saujanya verma
saujanya verma - avatar
6 odpowiedzi
0
I has already been incremented by ++I by the time you reach I++, so the expression is 4 + 4. I++ then sets I to 5, and then I = 4 + 4 = 8 overwrites that change. You won't find stuff like i = i++ anywhere but in programming exercises.
9th Sep 2016, 3:23 PM
Zen
Zen - avatar
0
I=3 ++I pre increment so I=4, now expression is I= 4 + 4 (I++)= 8 since I++ post increment is there so I value which is result of expression is incrementEd by 1 now I =9, it is the output
9th Sep 2016, 6:06 PM
venkatesh bitra
venkatesh bitra - avatar
0
9 is the output
10th Sep 2016, 4:18 AM
Soutik
Soutik - avatar
0
This result only dev c++ compilers. Another compilers output 8.
14th Oct 2016, 10:56 AM
Sirojiddin Nuriyev
Sirojiddin Nuriyev - avatar
0
++I da u 4ga teng bo'ladi. 4+I++ da 8 ga teng.
4th Apr 2017, 4:40 PM
Firdavs Beknazarov
Firdavs Beknazarov - avatar
0
it's called "undefined behavior".What will happen?It all depends on your compiler, so different compiler different answer. There is no need to think too much about these problem.
28th Jul 2017, 2:54 AM
Raindragon.DGJ
Raindragon.DGJ - avatar