+ 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?
4 Antworten
+ 2
b = 3
cout << ++b; -> 4
+ 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
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.
0
Thank you, very helpful