Prefix / Postfix

The use of the shorthand ++ incrementer or - - decrementer, either before (Prefix) or after (Postfix) the variable it is being applied to.


Prefix:  ++myVar


Postfix:  myVar++


Prefix will increment the variable before it is used


Postfix will use the variable before it is incremented, then increment the variable


If used on their own, the result is identical. It is only when combined with other expressions that differences appear:

#include <stdio.h>

int main( )

	int a, b, myVar = 5 ;

	printf("myVar starts off as: %d \n",myVar) ;

	a = ++myVar ;

	printf("Prefixing myVar makes a: %d \n", a) ;
	printf("myVar is now: %d \n", myVar) ;

	b = myVar++ ;

	printf("Postfixing myVar makes b: %d \n", b) ;
	printf("myVar is now: %d \n", myVar) ;

   return 0;

Compile & run:

myVar starts off as: 5
Prefixing myVar makes a: 6
myVar is now: 6
Postfixing myVar makes b: 6
myVar is now: 7

Leave a Reply