For working professionals
For fresh graduates
More
5. Array in C
13. Boolean in C
18. Operators in C
33. Comments in C
38. Constants in C
41. Data Types in C
49. Double In C
58. For Loop in C
60. Functions in C
70. Identifiers in C
81. Linked list in C
83. Macros in C
86. Nested Loop in C
97. Pseudo-Code In C
100. Recursion in C
103. Square Root in C
104. Stack in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
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!
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.