Pre-increment and Post-increment in C
The rich set of operators is supported by the c language. In c there are several operators used. The mathematical operations in the programming language are done with the operators to manipulate data and variables. The different types of operators are:
There are many operators used in the c language among them increment and decrement operators are used. The operand or variable which has the location in the memory for that operands only we can apply the increment operator.
Increment
In any c programming language for increasing an operand by 1, we use the increment operator. Increment operator represented by ” ++”. Most widely these operators are used in for and while loops.
The increment operator uses only one operand so that is why they are the unary operators. Increments operator is of two types they are pre-increment and post-increment.
- Pre increments:
Incremented and then assigned i.e, the operand value is incremented by 1 before the value is assigned to the operand. Represented by ++operator.
In simple, we can say that change and use;- Change the value of a variable.
- Use the new value.
- Post increments:
Assigned and then incremented i.e, the operand value is incremented by 1 after the value assigned to the operand. Represented by operator++.
In simple, we can say that use and change;- Use the original value of a variable.
- Change the value of a variable.
When statements are formed independently then ++operator and operator++ mean the same thing, when they are used in expressions on the right side of an assignment statement they behave differently.
For example:
Consider a=6;
b=++a;
From the above example, the value of b and a is 7 because it follows the increment and then assigns i.e,first 1was added to the a, and then it assigned the result to the b on left in the pre-increment.
a=6;
b=a++;
from the above case, the value of b is 6, and a will be 7 because it follows assign and then increment i.e, assigned the value to the b on the left first, and then a value is incrementedin the post-increment.
In subscripted variables when we use the increment operator i.e,++, the statement a[i++]=10; is equivalent to
a[j]=10;
j=j+1;
even using pre or post forms the value cannot be changed for the stand-alone operands.
Simple Programs for pre-increment and post-increment
Pre-increment:
#include<stdio.h>
Int main()
{
Int i=10,j;// vaiable declaration
j=++i;//pre-increment
printf(“i=%d\n”,i);
printf(“j=%d\n”,j);
return 0;
}
Output:
i=10
j=10
Post-increment:
#include<stdio.h>
Int main()
{
Int i=10,j;// vaiable declaration
j=i++;//post-increment
printf(“i=%d\n”,i);
printf(“j=%d\n”,j);
return 0;
}
Output:
i=11
j=10
Important Points
- When comparing post-increment and pre-increment, pre-incrementis faster this is because post-increment remains a copy of the old value and adds one to the existing value while pre-increment simply adds on without keeping the existing value.
- These operators are mostly used in the loops and in arrays to increase the index values.
- Compare to addition operations increment operations are efficient.
- In the case of n++ number operations are performed by the CPU in its Machine code compared to the n=n+1.
- They are applied only to the variables. And not used with the boolean values, not allowed the nesting of operators.
- In the precedence table the pre-increment is in the first row and then the second row is post-increment.
Examples of pre-increment and post-increment using loops
For loop
#include<stdio.h>
int main()
{
int number; //for loop to print 1-10 numbers
for(number=1;number<=10;number++) // post-increment
{
printf("%d\t",number); //to print the number
}
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
For loops produce identical results even if we use post-increment and the other pre-increment.
Examples of pre-increment and post-increment using an array
#include<stdio.h>
Int main()
{
int x,y,z;
int a[5]={1,2,3,4,5};
x=++a[1];// pre-increment
y=a[1]++;// post-increment
z=a[x++];
printf(“%d\n%d\n%d\n”,x,y,z);
return 0;
}
The above example is easy to solve but may get the wrong solution sometimes.so we need to understand it properly. Before that, there is an important point to be noted that is mentioned above. i.e
- The pre-increment is a higher precedence than the post-increment
Understanding the example:
First step :x = ++a[1]; // if a[1]=2 then it will be incremented and stored in x.
x and a[1] is 3.
second step :y=a[1]++;
This line is a tricky part of the program.
/*because of post-increment, first, y is assigned with a[1] and then incremented and stored in a[1].*/
a[1] value is 4, and y value is 3.
Third step :z =a[x++];
//because x is post-increment, the x value is used first and incremented later.
So, a[3] is stored z.