0

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

+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 😉

0

TheWh¡teCat can u explain this

0

TheWh¡teCat but what about the increment operator after assignment

0

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

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/