INCREMENT AND DECREMENT OPERATOR
In C, the
increment (++) and decrement (--) operators are used to increase or decrease
the value of a variable by one, respectively. These operators can be applied to
both integer and floating-point variables.
1. Increment
Operator (++):
The
increment operator (++) increases the value of a variable by one.
Example:
int x = 5;
x++; // x becomes 6
In this
example, the value of `x` is incremented by one using the increment operator.
2. Decrement
Operator (--):
The
decrement operator (--) decreases the value of a variable by one.
Example:
```c
int y = 7;
y--; // y becomes 6
```
In this
example, the value of `y` is decremented by one using the decrement operator.
Both the
increment and decrement operators can be used in different contexts:
-
Pre-increment and Pre-decrement: When the increment or decrement operator is
placed before the variable (++x or --x), it is called pre-increment or
pre-decrement. The value of the variable is first incremented or decremented,
and then the updated value is used in the expression.
-
Post-increment and Post-decrement: When the increment or decrement operator is
placed after the variable (x++ or x--), it is called post-increment or
post-decrement. The current value of the variable is used in the expression,
and then the value is incremented or decremented.
Example:
```c
int a = 5;
int b = ++a; // pre-increment: a becomes 6, b is assigned
the value of a (6)
int c =
a--; // post-decrement: c is assigned
the current value of a (6), then a becomes 5
```
It's
important to note that using the increment or decrement operators multiple
times within the same expression can lead to undefined behavior. Also, the
increment and decrement operators have a higher precedence than most other
operators, so their effects can be observed immediately.