Pages

23 Apr 2012

Post increment vs Pre increment

Every time when the post increment operator is used it performs two tasks firstly it stores the value of the variable in temporary location (say in a register) and then it increments the value of the variable and save it to the memory location. The incremented value is then transferred from the register to wherever it was assigned.

Apart from that the pre increment does not have to transfer the value That's why it is quite faster than the post increment operator


//pre increment
int a = 1;
int b = ++a; // Now a is 2 and b is also 2.


//post increment
a = 1;
int b = a++; // Now a is 2 but b is 1.

No comments:

Post a Comment