- 2

What is the output of the following code?

int a=3; int b=2; b=a++; cout<<++b; How is it 4:|

29th Jan 2017, 9:31 PM
Ammar
Ammar - avatar
7 Respuestas
+ 6
++a is pre_increment(first increments then assigns the value) , a++ is post increment(first assigns then increments) . For (b=a++) b gets the value of 'a' i.e 3. (++b) increments the value of b by 1 so the value of 'b' becomes 4.
30th Jan 2017, 1:10 PM
Ahmed Shah Malik
Ahmed Shah Malik - avatar
+ 6
Mr. Memol the question was how is it 4 :p
30th Jan 2017, 1:13 PM
Ahmed Shah Malik
Ahmed Shah Malik - avatar
+ 3
It's 4, first you set b=a++=3 (if it's b=++a=4) and then you do ++b so it equals 4
30th Jan 2017, 12:53 AM
Wiola
Wiola - avatar
0
The output is 4
29th Jan 2017, 10:27 PM
memol
0
bumping this how is it 4
29th Jan 2017, 11:12 PM
david schlemmer
david schlemmer - avatar
0
The output must be 3 and not 4
29th Jan 2017, 11:52 PM
Mohammed Maaz
Mohammed Maaz - avatar
0
Mr. Mohsin I didn't attention it. Besides he asked first "What is the output of the following code?" Ammar I explain sth and if you don't understand again,ask me. If a=3 and in a code you type a++ and type cout<<a; the output again is 3. But if you type ++a the output is 4. In first part if you want to print a for second time the output is 4. In this example when you say b=a++, b equals 3. And when you say cout<<++b; b equals 4.
3rd Feb 2017, 5:34 PM
memol