I'm a beginner | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I'm a beginner

What is the output of the following code? int a=3; int b=2; b=a++; cout<<++b; In this code, is b=2 no longer relevant because b now = the value of a?

22nd Jan 2017, 4:52 AM
Matthew Luvera
4 Answers
+ 2
b = 3 cout << ++b; -> 4
22nd Jan 2017, 5:10 AM
Kawaii
Kawaii - avatar
+ 1
int a=3; a is 3 int b=2; b is 2 b= a++; b is 3 and a is 4 cout <<++b; cout <<4
22nd Jan 2017, 6:05 AM
Didi Georgel Danaila
Didi Georgel Danaila - avatar
0
Yeah, b=2 is no longer relevant. Once it reaches b=a++, b is equal to three, and then a is incremented by one. So at the third line, a = 4, and b = 3. Then at the fourth line, b is incremented by one before any operations are done with it. So the output is 4.
22nd Jan 2017, 4:55 AM
DaemonThread
DaemonThread - avatar
0
Thank you, very helpful
22nd Jan 2017, 5:08 AM
Matthew Luvera