Open In App

Pre-Decrement and Post-Decrement in C

Last Updated : 06 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

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.

Syntax of Pre-Decrement

--variable_name

For example,

var = --a;

The  above statement is equivalent to:

a = a - 1;
var = a;

Let’s understand the pre-decrement operator with the help of a C program.

Example of Post-Decrement

C




// C program to illustrate the predecrement operation
#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 = 4

Explanation: In this example, we define an integer ‘num1’ with value 5. After that, we decrement the ‘num1’ and store the value of num1 in num2. whenever we use a pre-decrement operator it decrements the value of the variable first and then returns that value. Here firstly num1 is decremented by 1 and then store in the variable num2. So, values of both num1 and num2 become 4 after decrement as seen in the output.

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.

Example of Pre-Decrement and Post-Decrement

C




// C Program to illustrate the predecrement and post
// decrement
#include <stdio.h>
  
int main()
{
  
    int num1 = 5, num2 = 10;
  
    int postNum = num1--; // Post Decrement
    int preNum = --num2; // Pre Decrement
  
    // Printing value of postNum
    // after post decrement.
    printf("postNum = %d \n", postNum);
    printf("num1 = %d", num1);
    
    // Printing new line
    printf("\n");
    
    // Printing value of preNum
    // after pre decrement.
    printf("preNum = %d \n", preNum);
    printf("num2 = %d", num2);
  
    return 0;
}


Output

postNum = 5 
num1 = 4
preNum = 9 
num2 = 9

Explanation: In the above example, we have applied post decrement on ‘num1’ and pre decrement on ‘num2’ and assigned their value to ‘postNum’ and ‘preNum’ respectively. In the output, we can see that value of ‘postNum’ is 5 which is not the decremented value of ‘num1’ because post decrement assigns the value before the decrement value, and when we print the value of ‘num1’ it is decreased by 1. On the other side value of ‘preNum’ and ‘num2’ is 9 which is decreased by 1 because pre decrement operator first decreased the value by 1 and then assigns its value to another variable or used that value in an expression.

Precedence and Associativity of Pre-decrement and Post-Decrement

  • The precedence of post-decrement is more than pre-decrement post-decrement comes under the postfix category and pre-decrement is treated as unary operator.
  • The associativity of pre-decrement is from right to left while that of post-decrement is from left to right.

FAQs on Pre-Decrement and Post-Decrement in C

Q1. What happens when we use pre-decrement and post-decrement with numeric literals?

Answer:

The use of decrement operator with constants results in error because the decrement operator only works on rvalues (the values to which we can assign some constant values) such as variables and numeric literals are rvalues.

Example:

C




// C program to demonstrate the use of decrement operator
// with literals
#include <stdio.h>
  
int main()
{
  
    int a, b;
  
    a = 10 --;
    b = --10;
  
    printf("a=%d, b=%d", a, b);
  
    return 0;
}


Output

sum.c: In function 'main':
sum.c:7:9: error: lvalue required as decrement
operand
a = 10--;
^~
sum.c:8:7: error: lvalue required as decrement
operand
b = --10;

In the above example, we have applied the decrement operator to literals and we get the error “lvalue required as decrement operand” which means a variable should be used so that decremented value is stored in that variable because “10 = 10 -1” is a wrong statement and we know that “a–” is equivalent to “a = a-1”.

Q2. What is the difference between Pre-Decrement and Post-Decrement in C?

Answer:

There are only two major differences between the pre-decrement and post-decrement operators in C. They are as follows:

  1. Syntax: The pre-decrement is added as prefix to the variable, while the post-decrement is appended as suffix.
  2. Operation: The dercement is immediately performed in case of pre-decrement while in post-decrement, all the other operations are performed first, only then decrement is done.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads