How to use Post-Increment and Pre-Increment Expression in Same Statement ?

Postfix and Prefix Expression in Same Statement

#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,j=0;
j = i++ + ++i;
printf("%d\n",i);
printf("%d\n",j);
}

Output :

2
2


Explaination ?

j = i++ + ++i;
  1. Above Statement Contain in all 4 Operators , [ = , Post Increment , Pre Increment , + ]
  2. According to Precedence there Order is -
Operator Precedense Rank
Pre Increment 1
Post Increment 2
+ 3
= 4

 

  1. It is very interesting to know that , Which one gets executed first Pre-Increment , Post-Increment ? Refer Operator Associativity Unary Operator’s associativity is from [ R->L] so PreIncrement Operation gets executed first
  2. Here Pre Increment Operation uses new Value o ‘i’ so [ i = 1 ].
  3. This ‘i = 1′ is used for Post Increment Operation which uses Old Value i.e [i = 1] .
  4. Both ’1′s are added and value 2 is assigned to ‘j’.