![]() |
The increment ( ++ ) and decrement ( — ) operators in C are unary operators for incrementing and decrementing the numeric values by 1 respectively. The incrementation and decrementation are one of the most frequently used operations in programming for looping, array traversal, pointer arithmetic, and many more. In this article, we will discuss the increment operator and decrement operator, both their prefix and postfix applications, and the difference between them. Increment Operator in CThe 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. Syntax of Increment OperatorIncrement Operator can be used in two ways which are as follows: // AS PREFIX ++m // AS POSTFIX m++ where m is variable. How to use the increment operator?Both pre-increment and post-increment increase the value of the variable but there is a little difference in how they work. 1. Pre-IncrementIn 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. Example result = ++var1;
The above expression can be expanded as var = var + 1;
result = var;
2. Post-IncrementIn 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. Example result = var1++;
The above expression is equivalent result = var;
var = var + 1;
Example of Increment OperatorC
Output
Prefix Increment: 6 Postfix Increment: 5 As we can see in postfix, the value is incremented after the assignment operator is done.
Decrement Operator in CThe 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. SyntaxJust like the increment operator, the decrement operator can also be used in two ways: // AS PREFIX --m // AS POSTFIX m-- where m is variable. 1. Pre-Decrement OperatorThe 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. Example result = --m;
which can be expanded to m = m - 1;
result = m;
2. Post-Decrement OperatorThe 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. Example result = m--;
The above expression can be expanded as result = m;
m = m-1;
Example of Decrement OperatorC
Output
Prefix = 4 Postfix = 5 Differences between Increment And Decrement OperatorsThe following table list the difference between the increment and decrement operators:
|
Reffered: https://www.geeksforgeeks.org
Difference Between |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 10 |