Decrement Operators in C – Pre Decrement and Post Decrement Operators with Examples

Spread the love

Decrement-operators-in-c-langauge-pre-decrement-post-decrement

Introduction:

In our previous article, We have discussed the Increment Operators in the C language. In today’s article, We are going to look into the Decrement operators in C language and different types of decrement operators in C programming language.

Decrement Operator in C Language :

  • The Decrement operators  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 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: 

In the above example, the variable y value will be 4. because a value is first decremented then it will assigned to variable 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:

In the example, The variable y value is 5. Because in post decrement operation the value of variable assigned first and then value of a will be Decremented. So the resulting value of y is 5. Also, the value of variable a after above expression is 4.

In the post-decrement operation, First Assignment happens then the decrement operation.

Example program 1 : C program to Understand decrement operators:

Program Output:

Example 2 : program to understand Decrement Operator in C:

Output:

Example 3 : Decrement Operator can not be used on Constants:

Output:

So it is clear that we can not use constants in decrement operators.

Also Read :

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. November 29, 2018

    […] Decrements Operators and Pre and post decrements […]

  2. June 20, 2021

    […] Decrements Operators and Pre and post decrements […]

Leave a Reply