- 2
What is the output of the following code?
int a=3; int b=2; b=a++; cout<<++b; How is it 4:|
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.
+ 6
Mr. Memol the question was how is it 4 :p
+ 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
0
The output is 4
0
bumping this how is it 4
0
The output must be 3 and not 4
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.