Pre Increment / Decrement in C

July 20, 2010 1 Comment » Hits : 203





Pre-Increment : y = ++ivar  Steps :

  1. Value of ivar is incremented 
  2. Then value of ivar is assigned to y

In Short :

  • When Prefix (++/–) is used with a variable in an Expression .
  • The variable is incremented/decremented by one  then  Expression is evaluated using the original value of variable
  • Here a is incremented /decremented before use

Live Example :

#include<stdio.h>void main(){int a,b,x=10,y=10;a = x++;b = ++y;                    // Pre-Incrementedprintf("Value of a : %d",a);printf("Value of b : %d",b);}

Output :

Value of a : 10Value of b : 11

Why Output is 11 in case of Pre-increment : 

  1. Pre (before) , Value of y is first incremented i.e y = 11
  2. Then y is assigned to b

Incoming search terms:

  • Centrone

    Hi my friend! I want to say that this article is amazing, nice written and include approximately all important infos. I would like to see more posts like this .