Increment/ Decrement operators are also called unary operators. Another name of these operators is also counter operator. These are two as ++ (increment operator) and — (Decrement Operator). Increment operators are used for incrementing the value one by one. Similarly decrement operator are used for decrementing the value one by one. These are further subdivided into two categories:
- Prefix Operator
- Postfix Operator
Prefix Operators
In C programming language prefix increment operator, first of all value will be incremented and then incremented value will be assigned to a variable. Similarly prefix decrement operator will decrement the value and then the decremented value will be assigned to the variable. The general way is represented as:
++v; --v; where v is variable
Postfix Operators
In C programming language postfix increment operator, first of all value will be assigned to the variable and then it will be incremented. Similarly in the postfix decrement operator, value will be assigned and then it will be decremented.
v++; v--; where v is variable
Take a look at the example below to see what’s the difference between postfix and prefix increment/ Decrement operators.
For ex – x is a variable having value 7 and then y is another variable which will be computed by using x variable.
x=7; x=7; y=++x;(prefix Increment) y=x++;(postfix increment) After Processing The value of y be 8 The value of y be 7 The value of x be 8 The value of y be 8
Similarly Decrement prefix/postfix operators work.