Properties of the Increment operator
There are some properties of the increment operator, as follows:
- The increment operator is used to increase the current value of the variable by 1.
- We can only use these operators with variables.
- It is the operator represented by the double plus (++) symbol.
- Pre-increment operator
- Post-increment operator
There are two types of the increment operators
Pre-increment operator
The pre-increment operator is denoted by the double plus (++a) notation, added in front of the variable's identifier. This operator is employed to increase the operand's value by 1 before incorporating it into a mathematical operation. Essentially, the variable's value is incremented initially, and the revised value is subsequently applied in the calculation.
Syntax
x = ++a;
In the given syntax, the variable 'a' is initially increased by 1 before being utilized in the expression.
Example 1: We will now generate a basic code snippet to demonstrate the utilization of the pre-increment operator in the C programming language.
Program1.c
#include <stdio.h>
#include <conio.h>
int main ()
{
// declaration of the variables
int a = 7;
int b = 2
// print the value of the increment operator
printf (" Use the pre-increment operator " );
printf (" The value of a is %d ", a);
printf (" The value of b is %d ", b);
// use pre-increment operator
b = ++a;
printf (" After using the pre-increment operator ");
printf (" The value of a is %d ", a);
printf (" The value of b is %d ", b);
return 0;
}
Output
Use the pre-increment operator
The value of a is 7
The value of b is 2
After using the pre-increment operator
The value of a is 8
The value of b is 10
Let's develop an additional program that demonstrates the utilization of the pre-increment operator within a mathematical expression.
Program2.c
#include <stdio.h>
#include <conio.h>
int main ()
{
// declare integer variables
int a, b, c, d, x;
// initialization of the variables
a = 5;
b = 7;
c = 12;
d = 15;
// use pre-increment operator in the mathematical expression
x = ++a + ++b + ++c + ++d;
printf ( " The value of x is: %d ", x);
// print the updated value of a, b, c, and d
printf (" \n The updated value of a = %d, b = %d, c = %d and d = %d ", a, b, c, d);
return 0;
}
Output
The value of x is: 43
The updated value of a = 6, b = 8, c = 13 and d = 16
Post-increment Operator
Post-increment serves as an incremental operation denoted by the double plus symbol (a++), which appears after the operand 'a'. Its function involves increasing the operand's value by 1 after it has been utilized in a mathematical computation. Essentially, the original value of the variable is applied in the expression initially, with the post-increment operator subsequently adjusting the operand value by 1.
Syntax
x = a++;
In the provided syntax, the variable x is assigned the value of operand 'a', following which the post-increment operator increments the value of 'a' by 1.
Example 1: We will now develop a basic program to demonstrate the application of the post-increment operator in the C programming language.
Program1.c
#include <stdio.h>
#include <conio.h>
int main ()
{
// declaration of the variables
int a = 7;
int b = 0;
// print the value of the increment operator
printf (" Before using the post-increment operator " );
printf (" \n The value of a is %d ", a);
printf (" \n The value of b is %d ", b);
// use post increment operator
b = a++;
printf (" \n\n After using the post-increment operator ");
printf (" \n The value of a is %d ", a);
printf (" \n The value of b is %d ", b);
return 0;
}
Output
Before using the post-increment operator
The value of a is 7
The value of b is 0
After using the post-increment operator
The value of a is 8
The value of b is 7
Example 2: Let's develop an additional program to demonstrate the utilization of the post-increment operator within a mathematical expression.
Program2.c
#include <stdio.h>
#include <conio.h>
int main ()
{
// declare integer variables
int a, b, c, d, x;
// initialization of the variables
a = 5;
b = 7;
c = 12;
d = 15;
// use post-increment operator in the mathematical expression
x = a++ + b++ + c++ + d++;
printf ( " The value of x is: %d ", x);
// print the updated value of a, b, c, and d
printf (" \n The updated value of a = %d, b = %d, c = %d and d = %d ", a, b, c, d);
return 0;
}
Output
The value of x is: 39
The updated value of a = 6, b = 8, c = 13 and d = 16
Program to use the Pre-increment and Post-increment Operator
Let's develop a basic program to demonstrate the application of the pre-increment and post-increment operators in the C programming language.
Program3.c
#include <stdio.h>
#include <conio.h>
int main ()
{
int x, y, z, exp;
printf (" Enter the value of x: ");
scanf (" %d", &x);
printf (" \n Enter the value of y: ");
scanf (" %d", &y);
printf (" \n Enter the value of z: ");
scanf (" %d", &z);
printf (" \n Before using the increment operator: ");
printf (" \n The original value of x: %d", x);
printf (" \n The original value of x: %d", y);
printf (" \n The original value of x: %d", z);
// use pre-increment and post-increment operator
exp = x++ + ++x + ++y + y++ + ++z;
printf (" \n\n After using the increment operator: ");
printf (" \n The result of the expression is: %d", exp);
printf (" \n The updated value of x: %d", x);
printf (" \n The updated value of y: %d", y);
printf (" \n The updated value of z: %d", z);
return 0;
}
Output
Enter the value of x: 7
Enter the value of y: 12
Enter the value of z: 15
Before using the increment operator:
The original value of x: 7
The original value of x: 12
The original value of x: 15
After using the increment operator:
The result of the expression is: 58
The updated value of x: 9
The updated value of y: 14
The updated value of z: 16