- Increment operator
++
is used to add1
to a variable. - Decrement operator
--
is used to subtract1
from a variable. - Increment & Decrement operators in their so-called prefix or postfix forms.
Prefix operators.
The prefix operator increments/decrements the value of a variable before the variable is used in an expression.
++var_name
– prefix ++
operator
--var_name
– prefix --
operator
Postfix operators.
When used as a postfix operator, the program evaluates a variable in an expression and then increments its value.
var_name++
– postfix++
operatorvar_name--
– postfix--
operator
#include <stdio.h> int main(void) { int x = 10; int y = 10; int myprefix = ++x; int mypostfix = y++; printf("Prefix: %d, Postfix: %d\n", myprefix, mypostfix); }
- We have two int variables, x, and y, both having a value of 10.
- We use the prefix ++ operator on x.
- The x is incremented by one before the result of an expression is assigned to my prefix variable.
- Then, we use a postfix operator on y.
- The result of an expression is assigned to my postfix var, and then the value is incremented by one.