Increment Operator
Increment Operators are unary operators employed to increase or add 1 to the value of the operand. The Increment operand is represented by the double plus symbol (++). It consists of two varieties: Pre Increment and Post Increment Operators.
Pre-increment Operator
The pre-increment operator increments the operand's original value by 1 before assigning it to the expression.
Syntax
X = ++A;
In the given syntax, the operand 'A' is incremented by 1, followed by assigning a fresh value to the variable 'B'.
Example 1: Demonstration of utilizing the pre-increment operator in the C programming language.
#include <stdio.h>
#include <conio.h>
int main ()
{
// declare integer variables
int x, y, z;
printf (" Input the value of X: ");
scanf (" %d", &x);
printf (" Input the value of Y: ");
scanf (" %d", &y);
printf (" Input the value of Z: ");
scanf (" %d", &z);
// use pre increment operator to update the value by 1
++x;
++y;
++z;
printf (" \n The updated value of the X: %d ", x);
printf (" \n The updated value of the Y: %d ", y);
printf (" \n The updated value of the Z: %d ", z);
return 0;
}
Output
Input the value of X: 10
Input the value of Y: 15
Input the value of Z: 20
The updated value of the X: 11
The updated value of the Y: 16
The updated value of the Z: 21
Post increment Operator
The post-increment operator is utilized to increase the initial value of the operand by 1 after it has been assigned to the expression.
Syntax
X = A++;
In the provided syntax, the variable 'X' is assigned the value of operand 'A'. Subsequently, the value of variable 'A' is increased by 1.
Example 2: Code snippet demonstrating the application of the post-increment operator in the C programming language.
#include <stdio.h>
#include <conio.h>
int main ()
{
// declare integer variables
int x, y, z, a, b, c;
printf (" Input the value of X: ");
scanf (" %d", &x);
printf (" Input the value of Y: ");
scanf (" %d", &y);
printf (" Input the value of Z: ");
scanf (" %d", &z);
// use post-increment operator to update the value by 1
a = x++;
b = y++;
c = z++;
printf (" \n The original value of a: %d", a);
printf (" \n The original value of b: %d", b);
printf (" \n The original value of c: %d", c);
printf (" \n\n The updated value of the X: %d ", x);
printf (" \n The updated value of the Y: %d ", y);
printf (" \n The updated value of the Z: %d ", z);
return 0;
}
Output
Input the value of X: 10
Input the value of Y: 15
Input the value of Z: 20
The original value of a: 10
The original value of b: 15
The original value of c: 20
The updated value of the X: 11
The updated value of the Y: 16
The updated value of the Z: 21
Decrement Operator
The Decrement Operator functions as a unary operator that diminishes the operand's initial value by 1. It is denoted by the double minus symbol (--). This operator comes in two variations: Pre Decrement and Post Decrement operators.
Pre Decrement Operator
The Pre Decrement Operator reduces the value of the operand by 1 before it gets assigned to the mathematical expression. Essentially, the initial value of the operand is decremented first, followed by assigning the new value to another variable.
Syntax
B = --A;
In the given syntax, the operand 'A' is decremented by 1, followed by assigning a fresh value to the variable 'B'.
Example 3: Illustration of the pre-decrement operator in C
#include <stdio.h>
#include <conio.h>
int main ()
{
// declare integer variables
int x, y, z;
printf (" Input the value of X: ");
scanf (" %d", &x);
printf (" \n Input the value of Y: ");
scanf (" %d", &y);
printf ("n Input the value of Z: ");
scanf (" %d", &z);
// use pre decrement operator to update the value by 1
--x;
--y;
--z;
printf (" \n The updated value of the X: %d ", x);
printf (" \n The updated value of the Y: %d ", y);
printf (" \n The updated value of the Z: %d ", z);
return 0;
}
Output
Input the value of X: 5
Input the value of Y: 6
Input the value of Z: 7
The updated value of the X: 6
The updated value of the Y: 7
The updated value of the Z: 8
Post decrement Operator:
The post decrement operator is employed to reduce the initial value of the operand by 1 following its assignment to the expression.
Syntax
B = A--;
In the provided syntax, the variable 'B' is assigned the value of operand 'A', followed by a decrement of 1 from the value of 'A'.
Example 4: Code demonstrating the utilization of the post decrement operator in the C programming language.
#include <stdio.h>
#include <conio.h>
int main ()
{
// declare integer variables
int x, y, z, a, b, c;
printf (" Input the value of X: ");
scanf (" %d", &x);
printf (" Input the value of Y: ");
scanf (" %d", &y);
printf (" Input the value of Z: ");
scanf (" %d", &z);
// use post-decrement operator to update the value by 1
a = x--;
b = y--;
c = z--;
printf (" \n The original value of a: %d", a);
printf (" \n The original value of b: %d", b);
printf (" \n The original value of c: %d", c);
printf (" \n\n The updated value of the X: %d ", x);
printf (" \n The updated value of the Y: %d ", y);
printf (" \n The updated value of the Z: %d ", z);
return 0;
}
Output
Input the value of X: 6
Input the value of Y: 12
Input the value of Z: 18
The original value of a: 6
The original value of b: 12
The original value of c: 18
The updated value of the X: 5
The updated value of the Y: 11
The updated value of the Z: 17
Example 5: Code to execute the pre-increment and pre-decrement operators
#include <stdio.h>
#include <conio.h>
int main ()
{
// declare integer data type variable
int i, j, x, y;
printf (" Enter the value of i " );
scanf (" %d", &i);
printf (" Enter the value of j " );
scanf (" %d", &j);
// use pre increment operator to update original value by 1
x = ++i;
printf (" After using the pre-incrementing, the value of i is %d \n", i);
printf (" The value of x is %d \n", x);
// use pre decrement operator to decrease original value by 1
y = --j;
printf (" After using the pre-decrementing, the value of j is %d \n", j);
printf (" The value of y is %d \n", y);
return 0;
}
Output
Enter the value of i
5
Enter the value of j
10
After using the pre-incrementing, the value of i is 6
The value of x is 6
After using the pre-decrementing, the value of j is 9
The value of y is 9
B = --A;
#include <iostream>
int main() {
int num = 5;
// Post-increment operator
std::cout << "Post-increment:" << std::endl;
std::cout << "Original num value: " << num << std::endl;
std::cout << "After post-increment: " << num++ << std::endl;
std::cout << "Final num value: " << num << std::endl << std::endl;
num = 5; // Reset num to 5
// Post-decrement operator
std::cout << "Post-decrement:" << std::endl;
std::cout << "Original num value: " << num << std::endl;
std::cout << "After post-decrement: " << num-- << std::endl;
std::cout << "Final num value: " << num << std::endl;
return 0;
}
When executed, this program will showcase the post-increment and post-decrement operations on a variable in C++.
#include <stdio.h>
#include <conio.h>
int main ()
{
// declare integer data type variable
int i, j, x, y;
printf (" Enter the value of i " );
scanf (" %d", &i);
printf (" Enter the value of j " );
scanf (" %d", &j);
// use post increment operator to update original value by 1
x = i++;
printf (" After using the post-incrementing, the value of i is %d \n", i);
printf (" The value of x is %d \n", x);
// use post decrement operator to decrease original value by 1
y = j--;
printf (" After using the post-decrementing, the value of j is %d \n", j);
printf (" The value of y is %d \n", y);
return 0;
}
Output
Enter the value of i 10
Enter the value of j 20
After using the post-incrementing, the value of i is 11
The value of x is 10
After using the post-decrementing, the value of j is 19
The value of y is 20
Difference between the Increment and Decrement Operator in C
| Increment Operator | Decrement Operator |
|---|---|
| It is used to increment the value of a variable by 1. | It is used to decrease the operand values by 1. |
| The increment operator is represented as the double plus (++) symbol. | The decrement operator is represented as the double minus (--) symbol. |
| It has two types: pre-increment operator and post-increment operator. | Similarly, it has two types: the pre-decrement operator and the post-decrement operator. |
| Pre increment operator means the value of the operator is incremented first and then used in the expression.The post-increment operator means the operand is first used in the expression and then performs the increment operation to the original value by 1. | Pre decrement means the value of the operator is decremented first and then assigned in the expression.Whereas the post decrement operator means the operand is first used in the expression and then performs the decrement operation to the operand's original value by 1. |
| Syntax for the pre increment operator: X = ++a;Syntax for the post increment operator: X = a++; | Syntax for the pre decrement operator: X = --a;Syntax for the post decrement operator: X = a--; |
| Both operators' works only to the single operand, not values. | Both operators' works only to the single operand, not values. |