Post increment in expression solving

Initial Values :
int i=0,j=0;
int k = 0;
int temp=3;
Depending Upon These Initial Values Read Carefully following Code  Snippets ,

1.Post Increment Verity No 1
for(k=0;k<5;k++)
  {
  j = i++;            
  printf("%d ",j);
  }
  • i++ is Post Increment.
  • Meaning : First Old value of  variable 'i' gets assigned 'j' and then 'i' gets incremented.
  • So output of this code is = [ 0 1 2 3 4 ]
2.Post Increment Verity No 2
for(k=0;k<5;k++)
  {
  printf("%d ",i++);
  }
  • Meaning  First Old value gets printed first and then 'i' is incremented.
  • So output of this code is = [ 0 1 2 3 4 ]
3.Post Increment Verity No 3
for(k=0;k<5;k++)
  {
  j = i++ + temp;
  printf("%d ",j);
  }
  • Meaning  First Old value of 'i' is added with temp and result of addition is assigned to 'j'
  • temp = 3
  • So output of this code is = [ 3 4 5 6 7]
4.Post Increment Verity No 4
for(k=0;k<5;k++)
  {
  j = i++ < temp;
  printf("%d\n",j);
  }
  • Meaning  First Old value of 'i' is compared with 'temp' . Then result is assigned to j, and new value is used in second iteration .
  • temp = 3
  • So output of this code is = [ 3 4 5 6 7]
5.Post Increment Verity No 5
for(k=0;k<5;k++)
  {
  j = i++;            
  printf("%d ",i);
  }
  • Old value of 'i gets reflected only within same expression 
  • In the above snippet Old value is assigned to 'j' . but in the next statement always 'new value' is used.
  • So while printing i Output will be = [1 2 3 4 5].
6.Post Increment Verity No 6
for(k=0;k<5;k++)
  {
  i = i++;            
  printf("%d ",i);
  }
  • Old value of 'i is assigned to 'i' itself.
  • And again value is incremented after assigning.
  • So while printing i Output will be = [1 2 3 4 5].
  • But this Type is not supported by many compiles .
Note : 
  1. Same things happens while using Post-Decrement.
  2. Post Increment is Unary Operator.
  3. Post-Increment Operator has Highest Priority


0 comments

Post a Comment

Your Feedback :This is Growing Site and Your Feedback is important for Us to improve the Site performance & Quality of Content.Feel Free to contact and Please Provide Name & Contact Email