Confusing increment operator | SoloLearn: Learn to code for FREE!


Confusing increment operator

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

7/8/2019 5:52:42 PM

Siddharth Jain

5 Answers

New Answer


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 😉


TheWh¡teCat can u explain this


TheWh¡teCat but what about the increment operator after assignment


It's because the value was assigned to "a" before the increment happened.


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