Difference between Pre-increment and Post-increment in C
Increment operators are the kind of operators that are used to increment the cost of the operand by way of 1. In different words, the increment operator is an operator that is used to increment the value of the variable in an expression.
The illustration of the increment operator is the (++) symbol which helps the value to extend by way of 1.
There are mainly two kinds of increment operators:
- Pre-increment operator
- Post-increment operator
Properties of the increment operator
There are some properties of the increment operator:
- Increment operators are used to increase the value by way of 1.
- The increment operator solely works with variables.
- Increment operators are represented by a double plus symbol (++).
Pre-increment Operators
As we all know, the pre-increment operators are used to increment the value by 1. In the pre-increment operator, first, we extend the value of the variable after that we use it in the expression.
The pre-increment operators are represented by a double plus symbol (++a). In the pre-increment operator, symbol usually append earlier than the variable name.
Syntax of Pre-increment operator in C
x = ++a;
Let's take an example to understand the pre-increment operator well.
Example 1:
// program to show the usage of pre-increment operator in C
#include <stdio.h>
int main()
{
int a, b;
//taking enter from the user
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
//printing the value of the increment operator
printf("Before the use of the pre-increment operator\n");
printf (" Before a is %d \n", a);
printf (" Before b is %d \n", b);
//use of pre-increment operator
b= ++a;
printf("After the usage of the pre-increment operator\n");
printf ("After a is %d \n", a);
printf ("After b is %d \n", b);
return 0;
}
Output:
Enter two numbers
5
9
Before the use of the pre-increment operator
Before a is 5
Before b is 9
After the usage of the pre-increment operator
After a is 6
After b is 6
Example 2:
// program the use of the pre-increment operator in the mathematical expression in C
#include <stdio.h>
int main()
{
int a,b,c,x;
//taking enter from the user
printf("Enter three values\n");
scanf("%d%d%d",&a,&b,&c);
//value of the variable earlier than the use of pre-increment operator
printf("Before the use of the pre-increment operator\n");
printf ("Before a is %d \n", a);
printf ("Before b is %d \n", b);
printf ("Before c is %d \n", c);
//use of pre-increment operator in a mathematical expression
x= ++a + ++b + ++c;
printf("After the use of the pre-increment operator\n");
printf ("The value of x is %d \n", x);
printf("The updated values of the variables\n");
printf ("The updated value of a is %d \n", a);
printf ("The updated value of b is %d \n", b);
printf ("The updated value of c is %d \n", c);
return 0;
}
Output:
Enter three values
2
4
6
Before the use of the pre-increment operator
Before a is 2
Before b is 4
Before c is 6
After the use of the pre-increment operator
The value of x is 15
The updated values of the variables
The updated value of a is 3
The updated value of b is 5
The updated value of c is 7
Post-increment Operators
The post-increment operator is used to increment the value by means of 1 after the use of any expression. In different words, in the post-increment operator, we use value first in the expression after that we increment the value.
The post-increment operator is represented by way of a double plus symbol (a++). Basically, post-increment is used to increment the value of the operand by using 1 after the use of in the mathematical expression.
Syntax of Post-increment operator in C
A == b++
Let's take an example to explain the post-increment operator well:
Example 1:
// program to show the usage of post-increment operator in C
#include <stdio.h>
int main()
{
int a,b;
//taking enter from the user
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
//printing the value of the increment operator
printf("Before the use of the post-increment operator\n");
printf ("Before a is %d \n", a);
printf ("Before b is %d \n", b);
//use of post-increment operator
b= a++;
printf("After the use of the post-increment operator\n");
printf ("Updated value of a is %d \n", a);
printf ("Updated value of b is %d \n", b);
return 0;
}
Output:
Enter two numbers
5
7
Before the use of the post-increment operator
Before a is 5
Before b is 7
After the use of the post-increment operator
Updated value of a is 6
Updated value of b is 5