Decrement Operator is the unary operator, which is used to decrease the original value of the operand by 1. The decrement operator is represented as the double minus symbol (--). It has two types, Pre Decrement and Post Decrement operators.
Pre Decrement Operator
-The Pre Decrement Operator decreases the operand value by 1 before assigning it to the mathematical expression. In other words, the original value of the operand is first decreases, and then a new value is assigned to the other variable.
Syntax
B = --A;
In the above syntax, the value of operand 'A' is decreased by 1, and then a new value is assigned to the variable 'B'.
Example-Program to demonstrate 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