Why does ++a + ++a gives different answer in C++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Why does ++a + ++a gives different answer in C++?

So I tried this simple block int a = 3; cout << ++a + ++a; I tried it in different compilers and I got 9 in some and 10 in the others. 9 seemed justified to me, but I could not wrap my head around why this gives 10. Please help.

18th Oct 2022, 11:57 AM
Pramod Pandey
Pramod Pandey - avatar
16 Answers
+ 8
I am not sure if they are using different precedence rule .. it does seem like UB im not completely sure why ..... ChillPill 🌶 sir ? Clang gives 9 and gcc gives 10 for clang it works like so (++a) + (++a) //9 for gcc its like this (++(a++))+a //10 https://en.cppreference.com/w/cpp/language/operator_precedence Clang ones seem like its wrong as postfix has more precedence.... Personal suggestion : never modify one variable twice in a single expression like this ....
18th Oct 2022, 2:21 PM
Prashanth Kumar
Prashanth Kumar - avatar
+ 7
I'm pretty sure that it is undefined behavior in the standard definition of the language, so the result is compiler-dependent.
18th Oct 2022, 1:17 PM
Brian
Brian - avatar
18th Oct 2022, 3:23 PM
Lisa
Lisa - avatar
+ 5
Not a direct answer to the question, but you might be interested in this thread as well: https://www.sololearn.com/Discuss/2882645/?ref=app
18th Oct 2022, 3:41 PM
Lisa
Lisa - avatar
+ 4
In c/c++, when the statement have sequence point issue then it produce side effects and the output is compiler dependent , because it is undefined by standards.. https://en.m.wikipedia.org/wiki/Sequence_point So it may evaluated as 4+5 or 5+5 or 5+4 Or ++(3+4) depends on compiler implementation.. edit: additionally : https://stackoverflow.com/questions/7721383/on-c-c-basically-what-things-are-compiler-dependent
18th Oct 2022, 4:24 PM
Jayakrishna 🇮🇳
+ 2
Yeah a month ago I was also stuck with same. Can you list me the compilers which gave you output 10.
18th Oct 2022, 12:40 PM
I am offline
I am offline - avatar
+ 2
Snehil Pandey SoloLearn also gives 10 and the interview bit and programiz compiler gives 10. The g++ that I use also gives 10 as output
18th Oct 2022, 12:45 PM
Pramod Pandey
Pramod Pandey - avatar
+ 2
rust wins again amiright ChillPill 🌶 ?
20th Oct 2022, 10:23 AM
Bot
Bot - avatar
+ 2
ChillPill 🌶 of course, this is not the place for such debates. i was merely joking.
20th Oct 2022, 12:03 PM
Bot
Bot - avatar
+ 2
ChillPill 🌶 looks like i passed the flags -Wrustacean -Wdefend ...
20th Oct 2022, 1:18 PM
Bot
Bot - avatar
+ 1
Prashanth Kumar you are invited to solve this issue
18th Oct 2022, 12:50 PM
I am offline
I am offline - avatar
+ 1
Prashanth Kumar Okay, thanks for clearing that out!
18th Oct 2022, 3:53 PM
Pramod Pandey
Pramod Pandey - avatar
+ 1
Depends on the compiler. Follow the one which follows fundamental concepts. Or you can put brackets in order to make it follow
19th Oct 2022, 11:57 PM
Kausik Kar
Kausik Kar - avatar
0
a=3 ++a=4 + ++a=5 =9 Different compilers may give different outputs but by programming logic, the answer should be 9.
19th Oct 2022, 6:27 AM
Sentimeren
Sentimeren - avatar
0
(++a) + (++a) is much safer
19th Oct 2022, 4:20 PM
Shout Seli
Shout Seli - avatar
0
The difference lies in which value is used in the rest of the expression containing it. With ++a, a is incremented first, and that is the value you get; with a++ you get the value of a, and a is incremented afterwards.
20th Oct 2022, 6:24 AM
Zarbee
 Zarbee  - avatar