0
How the valu of b=3 in this program.when a=3,b=++a then b=4
2 ответов
+ 2
here first b=2
then when the line comes,
a=b++
first the value of a becomes b so a=b=2 and then b increments and becomes 2+1=3
then when b=++a
first a increments so a=2+1=3 and so b also becomes a and so b=3
You might have a look at it:
https://www.sololearn.com/learn/CPlusPlus/1610/?ref=app
+ 3
1) int a,b=2;
2) a=b++;
3) b=++a;
4) cout<<b;
At line 1, we have a = 0 and b = 2
At line 2, a is assigned with the value of b. so a becomes 2.
Notice the value of b is assigned first to a and then gets incremented(this is post increment)
So a = 2 and b = 3
At line 3, now b is assigned with pre increment value of a that is a=2 is incremented to a=3 and assigned to b.
Now a = 3 and b =3
Line 4 prints the value of b which is 3.
Check these to know more about these post and pre increment thing
https://www.sololearn.com/discuss/2284550/?ref=app
https://www.sololearn.com/discuss/177252/?ref=app
https://www.sololearn.com/discuss/265539/?ref=app
https://www.sololearn.com/discuss/818948/?ref=app
https://www.sololearn.com/discuss/315483/?ref=app