#include <iostream> using namespace std; int main() { int x; int i=3; x=++i + i++; cout << x; return 0; } | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 3

#include <iostream> using namespace std; int main() { int x; int i=3; x=++i + i++; cout << x; return 0; }

How the output is 9

16th Jul 2016, 3:09 AM
s v sagar sagar
s v sagar sagar - avatar
3 Réponses
+ 1
x=++i + i++; lets go step by step first thing is ++i , it increments i's value before its 'used' so ( i=4) now i+i++ in the second i++, the value will be used first and then be incremented. i= 4+ 4 = 8 and now will be incremented = 9
16th Jul 2016, 3:52 AM
Mukul Kumar
Mukul Kumar - avatar
0
sagar is pointing out a malfunction. Compiler acts differently when using same variable with multiple increments. Take the following int x; int i = 3; int j = 4; x = ++i + j++; cout <<x; will give you 8.
16th Jul 2016, 10:48 AM
Dhinakaran Purushothaman
0
I too feel compiler acts differently.In some other compiler it would be 8 good question
16th Jul 2016, 3:04 PM
kamal joshi
kamal joshi - avatar