Increment and Decrement Operators in C (original) (raw)
The increment ( ++ ) and decrement ( -- ) operators in C are unary operators for incrementing and decrementing the numeric values by 1, respectively.** They are one of the most frequently used operators in programming for looping, array traversal, pointer arithmetic, and many more.
**Increment Operator in C
The **increment operator ( ++ ) is used to increment the value of a variable in an expression by 1. It can be used on variables of the numeric type, such as integer, float, character, pointers, etc.
The Increment Operator in C can be used in two ways, either as a prefix (pre-increment) or a postfix (post-increment).
**Syntax:
C `
// AS PREFIX ++m
// AS POSTFIX m++
`
where m is variable. Both pre-increment and post-increment increase the value of the variable but there is a little difference in how they work.
1. Pre-Increment
In pre-increment, the increment operator is used as the prefix. Also known as prefix increment, the value is incremented first according to the precedence and then the less priority operations are done.
C `
result = ++var1;
`
The above expression can be expanded as
C `
var = var + 1; result = var;
`
2. Post-Increment
In post-increment, the increment operator is used as the suffix of the operand. The increment operation is performed after all the other operations are done. It is also known as postfix increment.
C `
result = var1++;
`
The above expression is equivalent
C `
result = var; var = var + 1;
`
**Example of Increment Operator
C `
#include <stdio.h>
void preincrement(int x) { // PREFIX printf( "X = %d\n", x); int prefix = ++x; printf("Prefix Increment: %d\n", prefix); }
void postincrement(int x) { // POSTFIX printf( "X = %d\n", x); int postfix = x++; printf("Postfix Increment: %d", postfix); }
int main() { int x =5; preincrement(x); postincrement(x);
return 0;}
`
Output
X = 5 Prefix Increment: 6 X = 5 Postfix Increment: 5
As we can see in postfix, the value is incremented after the assignment operator is done.
**Note: The post-increment has higher precedence that pre-increment as it is postfix operator while pre-increment comes in unary operator category.
**Decrement Operator in C
The decrement operator is used to decrement the value of a variable in an expression. In the Pre-Decrement, the value is first decremented and then used inside the expression. Whereas in the Post-Decrement, the value is first used inside the expression and then decremented.
**Syntax
Just like the increment operator, the decrement operator can also be used in two ways:
C `
// AS PREFIX --m
// AS POSTFIX m--
`
where m is variable.
1. Pre-Decrement Operator
The pre-decrement operator decreases the value of the variable immediately when encountered. It is also known as prefix decrement as the decrement operator is used as the prefix of the operand.
C `
result = --m;
`
which can be expanded to
C `
m = m - 1; result = m;
`
2. Post-Decrement Operator
The post-decrement happens when the decrement operator is used as the suffix of the variable. In this case, the decrement operation is performed after all the other operators are evaluated.
C `
result = m--;
`
The above expression can be expanded as
C `
result = m; m = m-1;
`
**Example of Decrement Operator
C `
#include <stdio.h>
void predecrement(int x) { // PREFIX printf( "X = %d\n", x); int prefix = --x; printf("Prefix Decrement: %d\n", prefix); } void postdecrement(int x) { // POSTFIX printf( "X = %d\n", x); int postfix = x++; printf("Postfix Decrement: %d", postfix); }
// Driver code int main() { int x =5; predecrement(x); postdecrement(x);
return 0;}
`
Output
X = 5 Prefix Decrement: 4 X = 5 Postfix Decrement: 5
**Increment vs Decrement
**The following table lists the major differences between increment and decrement operators in C :
| Increment Operator | Decrement Operator |
|---|---|
| Increment Operator adds 1 to the operand. | Decrement Operator subtracts 1 from the operand. |
| The Postfix increment operator means the expression is evaluated first using the original value of the variable and then the variable is incremented(increased). | The Postfix decrement operator means the expression is evaluated first using the original value of the variable and then the variable is decremented(decreased). |
| Prefix increment operator means the variable is incremented first and then the expression is evaluated using the new value of the variable. | Prefix decrement operator means the variable is decremented first and then the expression is evaluated using the new value of the variable. |
| Generally, we use this in decision-making and looping. | This is also used in decision-making and looping. |