Increment and Decrement Operators in C
Increment and decrement operators in C are unary operators used to increase or decrease the value of a variable by one.
Increment Operator (++)
The increment operator increases the value of a variable by 1.
Example: a++;
Decrement Operator (--)
The decrement operator decreases the value of a variable by 1.
Example: a--;
Pre-Increment Operator (++a)
In pre-increment, the variable is incremented first and then its updated value is used in the expression.
Example: ++a;
Post-Increment Operator (a++)
In post-increment, the current value is used first and then the variable is incremented.
Example: a++;
Pre-Decrement Operator (--a)
In pre-decrement, the variable is decremented first and then its updated value is used.
Example: --a;
Post-Decrement Operator (a--)
In post-decrement, the current value is used first and then the variable is decremented.
Example: a--;
Key Points to Remember
- These operators work only with variables, not constants
- They change the value of the variable by exactly one
- Pre and post forms behave differently in expressions
- Avoid using multiple increment/decrement operators in a single expression
Codecrown