Increment and Decrement Operators in C
One of the most common operations in the C language is incrementing or decrementing a variable by one.
In the C language, there are two specific operators known as the increment operator and the decrement operator. In this lesson, we will see how they work and how to use them.
Increment and Decrement Operators
In the C language, just like in many programming languages, one of the most common operations is to increment or decrement the value of a variable by one.
In C, it is possible to do this using the assignment operator:
int a = 0;
a = a + 1;
a = a - 1;
It is also possible to use compound assignment operators:
int a = 0;
a += 1;
a -= 1;
These operators are very convenient, but they are not the most used. In fact, C also provides the increment and decrement operators, which allow for even more concise code. These operators are the increment operator ++
and the decrement operator --
.
The increment operator ++
increases the value of the variable by one. The decrement operator --
decreases the value of the variable by one. However, these two operators can be used in two different ways:
- Postfix: the operator is applied after the variable
- Prefix: the operator is applied before the variable
We will now look at them in detail.
Postfix Increment and Decrement Operator
In the case of the postfix increment and decrement operators, the operator is applied after the value of the variable. For example:
int a = 0;
a++;
In this case, the value of a
is incremented by one after the value of a
is read. So, in this case, the value of a
is 1.
Similarly for the decrement operator:
int a = 0;
a--;
In this case, the value of a
is decremented by one after the value of a
is read. So, in this case, the value of a
is -1.
Since these operators are still assignment operators, they are also considered by the compiler as expressions that return a result. However, being postfix, the result of the expression is the value of the variable before the increment or decrement. For example:
int a = 0;
int b = 0;
b = a++;
In this case, the value of b
is 0, while the value of a
is 1. This is because the result of the expression a++
is the value of a
before the increment, i.e., 0. The same applies for the postfix decrement operator.
Postfix Increment and Decrement Operators
The postfix increment operator ++
increases the value of the variable by one. The postfix decrement operator --
decreases the value of the variable by one. The syntax is as follows:
a++;
a--;
The result of these expressions is the value of the variable before the increment or decrement.
Being assignment operators, they have side effects because they modify the value of the variable.
Let's look at an example of how these operators work.
int a = 5;
printf("%d\n", a++);
printf("%d\n", a);
The result of executing this code snippet is:
5
6
The first printf
prints the value of a
before the increment, so 5. The second printf
prints the value of a
after the increment, so 6.
Prefix Increment and Decrement Operator
In the case of the prefix increment and decrement operators, the operator is applied before the value of the variable. For example:
int a = 0;
++a;
Unlike the postfix case, here the value of a
is incremented by one before the value of a
is read. Therefore, the result of the expression ++a
is the value of a
after the increment, which is 1. The same applies to the prefix decrement operator.
For example:
int a = 0;
int b = 0;
b = ++a;
In this case, both the value of b
and the value of a
are 1. This is because the result of the expression ++a
is the value of a
after the increment, which is 1.
Prefix Increment and Decrement Operators
The prefix increment operator ++
increases the value of the variable by one. The prefix decrement operator --
decreases the value of the variable by one. The syntax is as follows:
++a;
--a;
The result of these expressions is the value of the variable after the increment or decrement.
Being assignment operators, they have side effects because they modify the value of the variable.
Let's look at another example of how these operators work.
int a = 5;
printf("%d\n", ++a);
printf("%d\n", a);
The result of executing this code snippet is:
6
6
The first printf
prints the value of a
after the increment, so 6. The second printf
does the same.
Using Increment and Decrement Operators in Expressions
Although these two operators are very convenient to use in practice, you should be careful when combining them in more complex expressions.
Let's look at an example:
a = 2;
b = 3;
c = a++ + ++b;
In this case, the result of the last expression is c = 6
. In fact, the postfix operator is applied to variable a
, so its original value, which is 2, is used in the expression. On the other hand, the prefix operator is used for variable b
, so its value after the increment, that is 4, is used in the expression.
In general, using increment and decrement operators in complex expressions can lead to unexpected results. Therefore, it is always better to avoid using them this way.
In any case, when using them in expressions, keep in mind that:
- Postfix increment and decrement operators are right-associative;
- Prefix increment and decrement operators are left-associative.
Avoid Combining Increment and Decrement Operators in Complex Expressions
A good programming practice is to avoid combining increment and decrement operators in complex expressions so as not to make the code hard to read.
In Summary
In this lesson, we studied two widely used operators in C programming: the increment and decrement operators.
In particular, we saw that these operators are very useful for incrementing or decrementing a variable’s value by one. We also saw that these operators can be used in two different ways: postfix and prefix.
Depending on how they are used, the result of these operators is different. Specifically, if the operator is used in postfix form, the result of the expression is the value of the variable before the increment or decrement. If it is used in prefix form, the result of the expression is the value of the variable after the increment or decrement.