Understanding Decrement Operator in C | Tutorial: Post Decrement and Pre Decrement Operators with Examples

Spread the love

Decrement Operator :

  • The Decrement operator Decrements the Value of the variable by 1 (by Subtracting 1 from it’s Current Value).
  • Decrement Operator is Unary operator. That means Decrement operator is operates on only one Operand.
  • Decrement Operator have Highest priority than all Binary Operators. Because it is unary operator and Unary operators have higher priority than all Binary operators. You can see Full priority table here C operators Priority table.
  • Decrement Operator can not work on Constants. it can be apply only on Variables.
  • Decrement Operator Denoted by — Symbol.
  • Ex : –X (pre decrement)  or X– (Post Decrement).

Different Types of Decrement operators :

We have two types of Decrement operators those are
  1. pre-Decrement operator (or) prefix decrement operator.
  2. post-Decrement operator (or) postfix decrement operator.

Pre-decrement Operator :

  • In pre-decrement operator, Operator is written before the operand.
  • Pre-decrement operator decrements the value of variable first, then decremented value is used to evaluate the Expression.
Ex: 
  1.  a= 5;
  2.  y = a;
In above example y value will be 4. because a value is first decremented then it will assigned to y. so y value is 4.

Post-Decrement Operator :

  • In post-decrement operator, Operator is written after the Operand.
  • Post-decrement operator first expression is Evaluated and then value of Variable is decremented. that means decremented value is not used in expression.
Ex:
  1.  a= 5;
  2.  y = a–;
in above example y value is 5. because in post decrement operator’s value first assigned and then Decremented so value of y is 5.but value of a after that expression is 4.

Example program 1 : C program to Understand decrement operator

  1. #include<stdio.h>
  2. int main(void)
  3. {
  4.         int x=8,y=10;
  5.         printf(“x=%dt,x);
  6.         printf(“x=%dt,–x);   /*Prefix decrement*/
  7.         printf(“x=%dt,x);
  8.        
  9.         printf(“y=%dt,y);
  10.         printf(y=%dt,y);   /*Postfix decrement*/
  11.         printf(“y=%dt,y);
  12.         return 0;
  13. }
output :

Example 2 : program to understand Decrement Operator in C

  1. #include<stdio.h>
  2. void main()
  3. {
  4. int a,b,c=5,d=5;
  5. = c–;
  6. = d;
  7. printf(“Value of a : %d”,a);
  8. printf(“Value of b : %d”,b);
  9. printf(“Value of c : %d”,c);
  10. printf(“Value of d : %d”,d);
  11. }

output :

Example 3 : Decrement Operator can not be used on Constants

  1. #include<stdio.h>
  2. void main()
  3. {
  4.         int a;
  5.         a = 10–;
  6. }
output :

Compilation error time: 0 memory: 2248 signal:0

So it is clear that we can not use constants in decrement operators.
Also Read :
  1. Increment Operator in C.
  2. Modulo operator in C explained with Examples.
  3. In depth examples on Arithmetic operators.
  4. Arithmetic operators in C language.
  5. Compilation Stages in C language.
  6. Identifiers in C language and Rules for naming Identifiers.
  7. Structure of C Language.

Venkatesh Macha

Hi Guys, I am Venkatesh Macha. I am a programmer and an Open Source enthusiast, Presently working as a Software Engineer. Apart from that, I love to explore new technologies and things.

You may also like...

1 Response

Leave a Reply