Post-Decrement in C

In post-decrement operation, the value is decremented after all the other operations are performed i.e. all the other operators are evaluated. The decrement operator is used as the suffix to the variable name.

Syntax of Post-Decrement

variable_name--

For example,

var = a--;

The above expression is equivalent to:

var = a;
a = a - 1;

Here, we can see the difference between pre-decrement and post-decrement operators.

Example of Post-Decrement

C




// C program to illustrate the post decrement
#include <stdio.h>
  
int main()
{
  
    int num1 = 5;
  
    // Store decremented values of num1
    // in num2
    int num2 = num1--;
  
    printf("num1 = %d", num1);
  
    // Printing new line
    printf("\n");
  
    printf("num2 = %d", num2);
  
    return 0;
}


Output

num1 = 4
num2 = 5

Explanation: This example is the same as example 1 but in this example, we have used the post decrement operator. The post-decrement operator first returns the value and then decremented the value by 1. We store the post-decrement value of num1 in num2 and then we print both num1 and num2 and we can see in the output the value of num1 is decremented by 1 but that decremented value is not stored in num2 so, the value of num2 is 5. This is because of the concept we discussed that it first returns the value and then decrements the value by 1.

Pre-Decrement and Post-Decrement in C

Pre-decrement and post-decrement are the two ways of using the decrement operator to decrement the value of a variable by 1. They can be used with numeric data type values such as int, float, double, etc. Pre-decrement and Post-decrement perform similar tasks with minor distinctions.

In this article, we will discuss the pre-decrement and post-decrement operators in C.

Similar Reads

Pre-Decrement in C

In pre-decrement operation, the decrement operator is used as the prefix of the variable. The decrement in value happens as soon as the decrement operator is encountered....

Post-Decrement in C

...

Example of Pre-Decrement and Post-Decrement

In post-decrement operation, the value is decremented after all the other operations are performed i.e. all the other operators are evaluated. The decrement operator is used as the suffix to the variable name....

FAQs on Pre-Decrement and Post-Decrement in C

...