Decrement Operators in C – Pre Decrement and Post Decrement Operators with Examples
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
- pre-Decrement operator (or) prefix decrement operator.
- 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 2 |
a = 5; y = --a; |
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:
1 2 |
a = 5; y = a--; |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include<stdio.h> int main(void) { int x = 8, y = 10; // Pre-Increment Example on variable 'x' printf("Before Pre-Increment x=%d \n", x); printf("At Pre-Increment x=%d \n", --x); /*Prefix decrement*/ printf("After Pre-Decrement x=%d \n", x); printf("----------------------------\n"); printf("Before Post-Increment y=%d \n", y); printf("At Post-Increment y=%d \n", y--); /*Postfix decrement*/ printf("After Post-Increment y=%d \n", y); return 0; } |
Program Output:
1 2 3 4 5 6 7 |
Before Pre-Increment x=8 At Pre-Increment x=7 After Pre-Decrement x=7 ---------------------------- Before Post-Increment y=10 At Post-Increment y=10 After Post-Increment y=9 |
Example 2 : program to understand Decrement Operator in C:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include<stdio.h> int main() { int a, b, c=5, d=5; // Let's apply Post-Decrement on 'c' variable a = c--; // Pre-Increment of variable 'd' b = --d; printf("Value of a : %d \n", a); printf("Value of b : %d \n", b); printf("Value of c : %d \n", c); printf("Value of d : %d \n", d); return 0; } |
Output:
1 2 3 4 |
Value of a : 5 Value of b : 4 Value of c : 4 Value of d : 4 |
Example 3 : Decrement Operator can not be used on Constants:
1 2 3 4 5 6 7 |
#include<stdio.h> int main() { int a; a = 10--; return 0; } |
Output:
1 2 3 4 |
decrement1.c:5:11: error: expression is not assignable a = 10--; ~~^ 1 error generated. |
So it is clear that we can not use constants in decrement operators.
Also Read :
- Start Here – Step by step tutorials to Learn C programming Online with Example Programs – SillyCodes
- Modulo operator in C explained with Examples.
- In depth examples on Arithmetic operators.
- Arithmetic operators in C language.
- Compilation Stages in C language.
- Identifiers in C language and Rules for naming Identifiers.
- Structure of C Language.
4 Responses
[…] Decrements Operators and Pre and post decrements […]
[…] Decrements Operators and Pre and post decrements […]
[…] Decrement the end index – end– […]
[…] the character at the index endis a white space, Then decrement the endby 1 – i.e […]