Difference between post increment, pre increment, pre decrement, post decrement operator in C


I will use the following C program to explain the post  and pre increment/ decrement .

#include <stdio.h>
main()
int i=0,j;
j=i++;
j=++i;
while (1) { i++; } // loop forever
}

Here we will see from above program how the i++ and ++i are handled, I will use PowerPC assembly language to explain you the concepts.

When you have C statments as below

i++;
++i;

For the above the corresponding assembly instructions generated by compiler are below

i++;
lwz  r9,12(r31)
addi r0,r9,1
stw  r0,12(r31)
++i;
lwz  r9,12(r31)
addi r0,r9,1
stw  r0,12(r31)

If you see for both the i++ and ++i the assembly code generated by the compiler is same. Does this means i++ and ++i are same for the compiler. Before going into details we will see explaination of the assembly language

lwz  r9,12(r31) --- Load value in memory location of i to register R9
addi r0,r9,1    --- R0 = R9 + 1
stw  r0,12(r31) --- Store value of register R0 back to memory location of i

So now you may think what is the use of post/pre incremeant, both does the same job.

Lets see how there two are diffrent from one another and when they will be usefull.

When you user i++ or ++i alone in your program both means same. but when you use it with assignment operatior its value is going to be different.

j=i++;
j=++i;

Above both are diffrent. Lets decode it from assembly to understand them.

j=i++; will give you the following assembly code

lwz  r0,12(r31) --- Load value in memory location of i to register R0
stw  r0,8(r31)  --- Store value of register R0 to memory location of j
lwz  r9,12(r31) --- Load value in memory location of i to register R9
addi r0,r9,1    --- R0 = R9 + 1
stw  r0,12(r31) --- Store value of register R0 back to memory location of i

In simple we can explain as follow

R0 = i
j = R0
R9 = i
R0 = R9 + 1
i = R0

Now lets see the case for j=++i, the assembly decode is below

lwz  r9,12(r31) --- Load value in memory location of i to register R9
addi r0,r9,1    --- R0 = R9 + 1
stw  r0,12(r31) --- Store value of register R0 back to memory location of i
lwz  r0,12(r31) --- Load value in memory location of i to register R0
stw  r0,8(r31)  --- Store value of register R0 to memory location of j

In simple we can explain as follow

R9 = i
R0 = R9 + 1
i = R0
R0 = i
j = R0

This is how it differs when you use the post/pre incremeant and assignment together.

The same applies to post/pre decremeant operations too.

Hope you can understand how post/pre increment/decrement operator in C works.

Tips: to generate Assembly code from your C program use the following commands for GCC compiler

$ gcc -S test.c
$ ls
test.c test.s

test.s is your assembly language file generated by compiler

Hope it is use full to someone.

About these ads

7 Comments

  1. Pingback: increment and decrement operator in C « Thalif's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s