Increment operator and decrement operator
Increment Operator in C:-
In C, you can add 1 to the value of a variable by using the increment operator (++). It stands for adding 1 to the variable's present value in shorthand notation. There are two ways to use the increment operator: as a pre and as a post.
- Pre Increment (
++variable
):-In the pre-increment form, the variable is incremented before its value is used in any expressions.
int a = 5;
int b = ++a; // Increment a first, then assign to b
// Now a is 6, b is also 6
- Post Increment (variable++):-In the post-increment form, expressions start with the variable's current value and then increment it.
int a = 5;
int b = a++; // Increment a first, then assign to b
// Now a is 6 and b is also 5
Decrement Operator in C:-
In C, a variable's value can be reduced by 1 using the decrement operator (--). The decrement operator can also be used as a pre or a post, just like the increment operator.
- Pre-Decrement (
--variable
): The variable is decremented in the pre-decremented form before its value is used in any expressions.
int a = 5;
int b = --a; // Increment a first, then assign to b
// Now a is 4 and b is also 4
- Post-Decrement (
variable--
): The variable's current value is used in expressions before being decreased in the postfix form.
int a = 5;
int b = a--; // Increment a first, then assign to b
// Now a is 4 and b is also 5