C Decrement Operator

In this tutorial, we will learn about Decrement Operator in C language, what are prefix and post decrements, with syntax and well detailed examples.

C Decrement Operator decrements the given value by one.

Decrement Operator takes only one operand. There are two forms for Decrement Operator based on the side of operator at which the operand is given, left of right. They are:

  • Prefix Decrement
  • Postfix Decrement

In terms of execution, the main difference between prefix and postfix decrements is that: Prefix decrements the variable value and then participates in the expression evaluation or statement execution. But postfix first lets the expression evaluation or statement execution complete and then decrements the value of the operand.

Prefix Decrement

For Prefix Decrement, the operand comes to the right of the operator. The syntax is given below.

</>
Copy
--operand

Example

In the following example, we take an integer and decrement it using prefix form.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int a = 4;
    printf("Before Decrement : %d\n", a);
    
    --a;
    printf("After  Decrement : %d\n", a);
}

Output

Before Decrement : 4
After  Decrement : 3
Program ended with exit code: 0

Postfix Decrement

For Postfix Decrement, the operand comes to the left of the operator.

</>
Copy
operand--

Example

In the following example, we take an integer and decrement it using postfix form.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int a = 4;
    printf("Before Decrement : %d\n", a);
    
    a--;
    printf("After  Decrement : %d\n", a);
}

Output

Before Decrement : 4
After  Decrement : 3
Program ended with exit code: 0

Postfix vs Prefix Decrement

Let us check the difference of postfix and prefix using an example program.

Example

In the following example, we shall perform postfix and prefix on different variables, and then look into how they act during execution.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int a = 4;
    printf("Before Post-Decrement : %d\n", a); //4
    printf("During Post-Decrement : %d\n", a--); //4
    printf("After  Post-Decrement : %d\n\n", a); //5
    
    int b = 8;
    printf("Before Pre-Decrement : %d\n", b); //4
    printf("During Pre-Decrement : %d\n", --b); //9
    printf("After  Pre-Decrement : %d\n", b); //9
}

Output

Before Post-Decrement : 4
During Post-Decrement : 4
After  Post-Decrement : 3

Before Pre-Decrement : 8
During Pre-Decrement : 7
After  Pre-Decrement : 7
Program ended with exit code: 0

Decrement Float Value

Decrement Operator can take a float variable as operand and decrease its values by 1.

Example

In the following example, we shall initialize a float variable with some value and decrement its value by 1 using -- operator.

main.c

</>
Copy
#include <stdio.h>

int main() {
    float a = 4.2;
    printf("Before Decrement : %f\n", a);
    
    --a;
    printf("After  Decrement : %f\n", a);
}

Output

Before Decrement : 4.200000
After  Decrement : 3.200000
Program ended with exit code: 0

Conclusion

In this C Tutorial, we learned what Decrement Operator does, how to use Decrement Operator in prefix or postfix form, with the help of examples.