C Increment and Decrement Operators Explained with Examples

2. Decrement Operator --:
The decrement operator -- is used to reduce the operand's value by one (1). This operator, otherwise known as the unary decrement operator, is signified by the symbol --.
The symbol -- indicates the decrement operator, through which an operand's value is decreased by one (1). The decrement operator (--) has two forms and can have two uses as follows:

a. Pre-decrement:
Before the use of a variable in an expression, this pre-decrement operator decreases its value by one. It is represented as --x, which indicates: the value of x is decreased by 1 before it is used in the expression. The translation of the expression --x would be x=x-1.
Example:
x=25
a=--x;
In the example, x is initialized with the value of 25. With the pre-decrement operator --x, the value of x is decreased before it is assigned to a. Thus, the value of a becomes 24.

b. Post-decrement:
This post-decrement operator decreases the value of a variable by one after having used it in an expression. It is represented by x--, meaning that the value of x is decreased by one after it was used in the expression.
Example:
x=25 a=x--;
In this example, the variable x is initialized with the value of 25. When post-decrement operator x-- is applied, the first value of x will be assigned to a, and later it will be decreased by 1. Therefore, in the expression a=x--, the value 25 is first assigned to a and after that, by 1 of x will be decreased. Hence, a=25 and x=24.


Example 1: C Language Program to show Working of decrement operator(--) in c Language


/* C Language Program to show Working of decrement operator(--) i.e. Pre-decrement and Post-decrement. */
#include <stdio.h>
int main ()
{
int x, y, a, b;       // Local Variable Declared
a = 10; x = --a;       /* pre decrement operator decrements the value of a by 1 and assigns to x */
printf ("\n Pre decrement Operator");
printf(“ \n here the value of x is decreased by 1.”);
printf (" \n The value of x is %d.", x);
printf (" \n The value of a is %d.", a);
b = 40;
y = b--;       /* post decrement operator assign value of b to variable y and decrements the value of variable b by 1 */
printf (" \n\n Post decrement Operator");
printf (" \n The value of y is %d.", y);
// Show the value of b
printf (" \n The value of b is %d.", b);
return 0;
}
Output
Pre decrement Operator
Here the value of x is decreased by 1
The value of x is 9.
The value of a is 9.
Post decrement Operator
The value of y is 40.
The value of b is 39

Program Explanation:

In this C program, we are exploiting the pre-decrement and post-decrement operators using four integer variables: x, y, a, and b.
1. Pre-decrement Operator (--a):
The pre-decrement operator --a decreases the value of variable a by 1 before it's used in an expression. This means that the decrement happens first, and then the updated value is assigned.
In the program:
We assign the value 10 to a.
The application of the pre-decrement operator --a decreases the value of a by 1, making a becomes 9 and assigns that value to x.
Hence, both x and a get a value of 9.
The program gives output as follows:
Pre-decrement Operator
Here, the value of x is decreased by 1.
The value of x is 9.
The value of a is 9.
2. Post-decrement Operator (b--):
The post-decrement operator b-- works this way. The value of current b is assigned to y and the current b value is decremented by 1.
In this program:
We start with b initialized to 40.
The operation b -- assigns the current value of b (40) to y and then decrements b to 39.
Consequently, y is 40 and b becomes 39 following the operation.
The output produced by the program is:
Post-decrement Operator
Value of y is 40.
The value of b is 39.


Previous Topic:-->> Assignment Operators || Next topic:-->>Relational Operators