int a=5;cout<<++a + a++;//what is answer? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

int a=5;cout<<++a + a++;//what is answer?

This question seldom comes in Challenges. Answer of this question is 13.I have checked it on SoloLearn Code playground and on Code blocks. It comes same. But I don't know why every time when I type 13 while challenging it said Wrong??

9th Oct 2016, 4:07 AM
Atif Iqbal
Atif Iqbal - avatar
13 Answers
+ 2
The correct answer is : undefined behavior. That is: the result depends. There is no rule in c++ that specifies the order of evaluation of operands of an operator. So i++ can be evaluated first this time and may be evaluated after ++i next time. You may be confused: why 1+2-3 is evaluated from left to right? The answer is: that's the associativity of operators, not the evaluation sequence. This is one of the few subtle things one should be aware of when programming with c++. Go to cppreference.com and look for Order of evaluation for more details. You will see the exact expression on the page. It is explicitly tagged as undefined behavior.
11th Oct 2016, 5:15 AM
Bill
Bill - avatar
+ 1
How it can be 12?when I saw this first time i agree with you i calculate it as 12.But why it comes 13 on software Tiago J Goncalves.
9th Oct 2016, 4:15 AM
Atif Iqbal
Atif Iqbal - avatar
+ 1
Code playground is wrong. A is 5 -> no question Cout << ++(increment a) a( give me the new value of a which is 6, now a is 6) + a(a is still 6) ++( increment a but the value is never printed because afyer you increment the code is not asking you to see the value) so tou have 12. Try to cout<<a; after the code you have it will give you 13.
9th Oct 2016, 4:21 AM
Tiago J Goncalves
Tiago J Goncalves - avatar
+ 1
I have another question: Cout<<++a + ++a; gives 14 Cout<<a++ + a++; gives 11 Why??
9th Oct 2016, 2:55 PM
Stanislas
+ 1
Ok i have the answer: Cout<<a++; first prints out the original a and then adds 1 to it. Cout<<++a; first adds 1 and then prints the result.
9th Oct 2016, 3:03 PM
Stanislas
+ 1
Code is read from left to right. When you have ++a the code increments and gives you the value of a. When you have a++ the code gives the actual value of a and jncrements but doest show to you the value of a because you are not asking for it. If you want to see the values of a after ++a + a++ just add the following line: cout << a. And you will see 13. Read the code from left to right.
10th Oct 2016, 5:04 AM
Tiago J Goncalves
Tiago J Goncalves - avatar
+ 1
This Question which i asked and share,now has been removed from SoloLearn Challenges.
10th Oct 2016, 4:10 PM
Atif Iqbal
Atif Iqbal - avatar
0
why not 13?
9th Oct 2016, 7:08 AM
nello
0
#include <iostream> using namespace std; int main() { int a=5; cout << ++a<<a++; return 0; } Output is 75, does a++ have happens first than ++a?
9th Oct 2016, 3:58 PM
Francis Tagarela
Francis Tagarela - avatar
- 1
++a + a++ for a=5: first we do ++a which means we add 1 To a before the opération so a become 6.then we add To 6 an other 6.so the result is 12.the opération a++ is done after the addition.so if we write cout<< ++a + a++ ;it gives 12 and cout<< a gives 7.
11th Oct 2016, 8:15 AM
boutheina
- 3
Output is 12
9th Oct 2016, 4:09 AM
Tiago J Goncalves
Tiago J Goncalves - avatar
- 3
12
9th Oct 2016, 6:23 AM
dixit
- 3
12
9th Oct 2016, 8:51 AM
boutheina