0

Confusing increment operator

int a=10; a = a++ + 10; printf("%d",a); Output Is 20 Please explain how

8th Jul 2019, 5:52 PM
Siddharth Jain
Siddharth Jain - avatar
5 Answers
+ 1
Siddharth Jain , it is because the post increment "++" operator. Post increment first evaluate the expression then increment it. It's exactly the opposite with pre increment => if you put "++" before the variable - first increment the value and then evaluate the expression. You can change it and you'll see the difference => with pre increment it's 21, post increment it is 20. Hope you understand it better 😉
8th Jul 2019, 6:25 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
0
TheWhÂĄteCat can u explain this
8th Jul 2019, 5:54 PM
Siddharth Jain
Siddharth Jain - avatar
0
TheWhÂĄteCat but what about the increment operator after assignment
8th Jul 2019, 7:23 PM
Siddharth Jain
Siddharth Jain - avatar
0
It's because the value was assigned to "a" before the increment happened.
8th Jul 2019, 7:43 PM
D_Stark
D_Stark - avatar
0
a++ is postfix. The postfix form uses the value of the variable first, before incrementing/decrementing it. If you change the code a bit, then an example of incriment postfix a ++ will be more obvious: int a = 10; int b = 0; b = a++ + 10; /* step 1: b=10+10; step 2: a=10+1; */ printf("%d",a); //Output Is 11 printf("%d",b); //Output Is 20 https://www.sololearn.com/learn/C/2917/
8th Jul 2019, 9:13 PM
Solo
Solo - avatar