Tutorial Playlist
C language provides a multitude of operators, each designed to manipulate and transform data. Among these, the increment and decrement operators in C are a fundamental part of the language, vital for the control of loops, and sometimes used for fine-tuning arithmetic computations.
Increment and decrement operators refer to unary operators that function by respectively adding or subtracting one, to or from their operand. These are most commonly used in 'for' loops and 'while' loops. Before we delve deeper into these operators, it's important to note that there are two types of these operators: prefix and postfix.
The prefix increment operator in C (++) increases the variable’s value by one before the operation is carried out. The general form is:
++variable; |
Let's consider a simple explanation of one of the increment operator in C examples.
#include <stdio.h> |
In the above program, the prefix increment operator is used. The variable a is first incremented and then printed. So, the output will be -
Value of a before increment: 5 |
The postfix increment operator (++) increases the variable’s value by one after the operation is carried out. The general form is -
variable++; |
Consider the following example.
#include <stdio.h> |
In this case, a is printed first, then incremented. Therefore, the output will be -
Value of a before increment: 5 |
The prefix decrement operator (--) decreases the variable’s value by one before the operation is carried out. The general form is -
--variable; |
Let's consider an example.
#include <stdio.h> |
Here, the value of a is decremented before it is printed. So, the output will be -
Value of a before decrement: 5 |
The postfix decrement operator (--) decreases the variable’s value by one after the operation is carried out. The general form is -
variable--; |
Here's an example.
#include <stdio.h> |
In this case, a is printed first, then decremented. Therefore, the output will be -
Value of a before decrement: 5 |
In any programming language, operators have a certain priority level which is known as precedence. In the case of the increment (++) and decrement (--) operators in C, they have one of the highest levels of precedence, right after the scope resolution operator, function call, array subscript, and member access operators. This high precedence is applicable to both prefix and postfix versions of these operators.
However, the timing of the operation differs between the prefix and postfix forms. Prefix increment/decrement (++a or --a) performs the operation before the value is implemented in the expression, while postfix increment/decrement (a++ or a--) performs the operation after the value is used in the expression.
This difference becomes crucial when these operators are used in more complex expressions.
Consider the following examples -
Example 1: Prefix increment operator
#include <stdio.h> |
In this case, a is first incremented to 6, and then the multiplication is carried out, resulting in b being 18. So, the output will be -
Value of a: 6 |
Example 2: Postfix increment operator
#include <stdio.h> |
In this case, the multiplication is carried out first, and then a is incremented. So, b becomes 15, and a becomes 6. Therefore, the output will be -
Value of a: 6 |
In both examples, the increment operator has the highest precedence in the expression, but the time at which the increment operation is performed varies depending on whether it is a prefix or postfix.
While the increment operator extends the value of its operand by one, the decrement operator lessens it by one. The difference between prefix and postfix operators lies in the timing of their operation in an expression - prefix operators change the operand before its value is used in the expression, while postfix operators change the operand after its value is used in the expression.
Here are the differences between prefix and postfix increment and decrement operators in a tabular form:
Prefix Increment (++a) | Postfix Increment (a++) | Prefix Decrement (--a) | Postfix Decrement (a--) | |
Operation | Increases a by 1 before a is used in the expression | Increases a by 1 after a is used in the expression | Decreases a by 1 before a is used in the expression | Decreases a by 1 after a is used in the expression |
Use Case | When you want to increment the value of a before it is used in the rest of the expression | When you want to increment the value of a but still use the original value in the rest of the expression | When you want to decrement the value of a before it is used in the rest of the expression | When you want to decrement the value of a but still use the original value in the rest of the expression |
In essence, while both increment and decrement operators change the value of their operand, the timing of the operation can influence the outcome of the larger expression in which they are used, providing you with more control and flexibility over your computations.
Here are some of the interesting and mostly confused (or often forgotten) facts about increment and decrement operators:
Understanding the intricacies of increment and decrement operators in C is a stepping stone to mastering the language, helping you easily solve increment and decrement operators in C questions. These operators allow for more efficient code by simplifying operations and optimising loops. As we've seen, both the increment and decrement operators come in two forms, prefix and postfix, each with its own unique behaviour in the context of an expression. Getting a good grip on these differences will enable you to write more flexible and efficient code in C.
While this article provides a good starting point, nothing beats hands-on practice. To dive deeper into C and other programming languages, consider enrolling in upGrad’s MS in Full Stack AI and ML Program powered by Golden Gate University. Designed by industry professionals, this course not only covers theory but also emphasises practical application and real-world scenarios.
Enroll today and take a significant step towards enhancing your programming skills!
PAVAN VADAPALLI
popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. .