Tutorial Playlist
132 Lessons1. Introduction to C Tutorial
2. Addition of Two Numbers in C
3. Anagram Program in C
4. Armstrong Number in C
5. Array in C
6. Array of Pointers in C
7. Array of Structure in C
8. C Program to Find ASCII Value of a Character
9. Assignment Operator in C
10. Binary Search in C
11. Binary to Decimal in C
12. Bitwise Operators in C
13. Boolean in C
14. C Compiler for Mac
15. C Compiler for Windows
16. C Function Call Stack
17. C Language Download
18. Operators in C
19. C/C++ Preprocessors
20. C Program for Bubble Sort
21. C Program for Factorial
22. C Program for Prime Numbers
23. C Program for String Palindrome
24. C Program to Reverse a Number
25. Reverse a String in C
26. C string declaration
27. String Input Output Functions in C
28. Calculator Program in C
29. Call by Value and Call by Reference in C
30. Ceil Function in C
31. Coding Vs. Programming
32. Command Line Arguments in C/C++
33. Comments in C
34. Compilation process in C
35. Conditional Statements in C
36. Conditional operator in the C
37. Constant Pointer in C
38. Constants in C
39. Dangling Pointer in C
40. Data Structures in C
41. Data Types in C
42. Debugging C Program
43. Convert Decimal to Binary in C
44. Define And include in C
45. Difference Between Arguments And Parameters
46. Difference Between Compiler and Interpreter
47. Difference Between If Else and Switch
48. Do While Loop In C
49. Double In C
50. Dynamic Array in C
51. Dynamic Memory Allocation in C
52. Enumeration (or enum) in C
53. Evaluation of Arithmetic Expression
54. Factorial of A Number in C
55. Features of C Language
56. Fibonacci Series Program in C Using Recursion
57. File Handling in C
58. For Loop in C
59. Format Specifiers in C
60. Functions in C
61. Function Pointer in C
62. goto statement in C
63. C Hello World Program
64. Header Files in C
65. Heap Sort in C Program
66. Hello World Program in C
67. History of C Language
68. How to compile a C program in Linux
69. How to Find a Leap Year Using C Programming
70. Identifiers in C
71. If Else Statement in C
72. If Statement in C
73. Implementation of Queue Using Linked List
74. Increment and decrement operators in c
75. Input and Output Functions in C
76. How To Install C Language In Mac
77. Jump Statements in C
78. Lcm of Two Numbers in C
79. Length of an Array in C
80. Library Function in C
81. Linked list in C
82. Logical Operators in C
83. Macros in C
84. Matrix multiplication in C
85. Nested if else statement in C
86. Nested Loop in C
87. One Dimensional Array in C
88. Operator Precedence and Associativity in C
89. Overflow And Underflow in C
90. Palindrome Program in C
91. Pattern Programs in C
92. Pointer to Pointer in C
93. Pointers in C: A Comprehensive Tutorial
94. Pre-increment And Post-increment
Now Reading
95. Prime Number Program in C
96. Program for Linear Search in C
97. Pseudo-Code In C
98. Random Access Files in C
99. Random Number Generator in C
100. Recursion in C
101. Relational Operators in C
102. Simple interest program in C
103. Square Root in C
104. Stack in C
105. Stack Using Linked List 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
112. String Comparison in C
113. String Functions in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
118. Structure of C Program
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
122. Toupper Function in C
123. Transpose of a Matrix in C
124. Two Dimensional Array in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
129. User Defined Functions in C
130. What is Variables in C
131. Is C language case sensitive
132. Fibonacci Series in C
Increment operators (++) in C are primarily used to increase a variable’s value by 1. They provide a convenient way to control loops, manipulate pointers and even simplify code, among various other functionalities. There are mainly two types of increment operators in C, namely,
While both pre-increment and post-increment in C/C++ are responsible for enhancing the overall readability and efficiency of the code, there are a few differences when it comes to how each of them operates.
On that note, let’s take a closer look at pre-increment and post-increment in C with examples. Additionally, you will also get to know some of the most commonly asked pre-increment and post-increment questions.
Pre-increment in C programming language refers to a specific type of operator that is used to enhance a variable’s value by one before the expression is valued. It is typically denoted by using the ‘++ symbol, which is placed before the variable.
Pre-increment in C is mostly used in loops and other similar situations wherein you have to increment a variable before using its updated value in an expression. It provides a convenient way to modify a variable’s value while simultaneously evaluating an expression.
The syntax for pre-increment in C includes,
++variable |
Let’s explore a few small pre-increment in C examples to help you get a better understanding of the same,
#include <stdio.h> |
Here, we have initially assigned value 5 to the ‘num’ variable. Following this, the pre-increment operator, ‘++num’, is used to add to the value of ‘num’ by one before the expression is evaluated. This will generate the following output.
Original value of num: 5 |
Pre-increment in An Assignment Statement
#include <stdio.h> |
Here, the ‘++num’ pre-increment operator is used in the assignment statement, ‘int result = ++num’. The value of ‘num’ is incremented by one before being assigned to ‘result’.
Output
num: 6 |
Pre-increment In An Expression
#include <stdio.h> |
Here, the expression ‘3 * (++x + y)’ uses the increment operator ‘++x’ to increment the value of ‘x’ by one before evaluating the expression.
Output
result: 24 |
Pre-increment In A Conditional Statement
#include <stdio.h> |
The ‘++count’ operator is used in the conditional statement of the ‘while’ loop, which then increments the value of ‘count’ before each iteration.
Output
Iteration 1 |
Pre-increment in A Function Argument
#include <stdio.h> |
Here, the increment operator, ‘++(*num), is leveraged to increase the value pointed to by the ‘num’ pointer within the function.
Output
Value before increment: 10 |
Although the pre-increment operator in C is pretty simple to use, a special case is involved when it comes to large expressions. This means that when you use a pre-increment operator within a larger expression, the result of that expression will directly be influenced by the original value of the variable being incremented. Let’s take a closer look at this through the small example mentioned below.
int num = 5; |
Here, the variable, ‘num’, is being incremented using the pre-increment operator within the ‘++num + num’ expression. The goal is to increment ‘num’ and then add it to the incremented value. However, according to the C programming language, if you modify the value of a variable and use the modified value within the same expression, it might generate undefined behaviour.
Therefore, in this case, we might not actually be able to achieve our goal since the specific outcome of this expression is not guaranteed.
To prevent the same from happening, it is always recommended that when using a pre-increment operator, you must do the same in isolation or separate it from the expression that relies on the original value. Therefore, instead of the above-mentioned code, you must apply the following algorithm,
int num = 5; |
As clearly visible, here we have created a well-defined distinction between the pre-increment operator and the expression. This not only ensures well-defined behaviour but also keeps any kind of potential issues that might threaten the order of evaluation at bay.
Contrary to the pre-increment operator in C, the post-increment operator is used to grow the value of any variable by one after the expression is evaluated. It, too, is denoted by the ‘++’ symbol, the only difference being, it is always placed after the variable. The syntax for the same goes as follows,
variable++ |
The post-increment operator in C is primarily used when you wish to use the current value of a variable within an expression and increment it afterwards.
Here are a few small examples highlighting how the post-increment operator works for varied scenarios in the C programming language.
Post-increment Operator In A Loop
#include <stdio.h> |
Here, ‘i’ refers to the name of our variable, and the ‘i++’ post-increment operator is used to enhance the value of this variable after each iteration of the loop. When you run this program, it will generate the following outcome.
1 2 3 4 5 |
Post-increment With Multiple Variables
#include <stdio.h> |
Here, we have two variables, namely ‘x’ and ‘y’. The post-increment operators, ‘x++’ and ‘y++’, are used to increment the values of both these variables after evaluating the expression.
Output
result: 8 |
Post-increment in An Expression
#include <stdio.h> |
In this example, the expression, ‘3 * (x++ + y), uses the post-increment operator, ‘x++’, to increment the value of variable ‘x’ after evaluation.
Output
result: 21 |
Post-increment In An Assigned Statement
#include <stdio.h> |
Here, the post-increment operator, ‘num++’, is used on the assigned statement, ‘int result = num++’. The value of ‘num’ is initially allotted to the ‘result’, following which ‘num’ is incremented.
Output
num: 6 |
Post-increment In A Conditional Statement
#include <stdio.h> |
The post-increment operator, ‘count++’, is applied on the conditional statement of the ‘while’ loop. The value of ‘count’ is then incremented after each iteration.
Similar to the pre-increment operator in C, the post-increment operator, too, has a special case when used as a part of a larger expression. When you assign the post-increment operator on a variable and assign it to the same variable, then the value will not be incremented. Let’s take a look at this short example.
#include <stdio.h> |
Output
x: 6 |
Here, we have the statement, ‘int y = x++’, where the post-increment operator is used to assign the value of two variables, namely, ‘x’ and ‘y’, and then increment the value of ‘x’. The special case of the post-increment operator states that the value used in the assignment is the original value of the variable before the increment takes place. Therefore, ‘y’ gets the original value of ‘x’ (5) before it is incremented to 6.
There is another significant difference when it comes to pre-increment and post-increment in C/C++. It is related to their associativity. The associativity of an operator basically determines the order in which the operations will perform when multiple operators of the same precedence exist in an expression.
By default, the pre-increment operator in C has a lower and right-to-left associativity, which means that the increment operation is performed before the surrounding expression is evaluated.
The post-increment operator, on the other hand, has a higher precedence and left-to-right associativity, which means that the surrounding expression is evaluated first, followed by the increment.
In order to accurately predict the behaviour of expressions and ensure the desired results in your code, having an in-depth understanding of the associativity of pre-increment and post-increment in C is, therefore, extremely crucial.
Hopefully, with this, you have gained a clear perception of what is post-increment and what is pre-increment in C. Both these increment operators are used to add to a variable’s value by 1. They serve multiple purposes and provide convenience in various programming scenarios. Such include arithmetic operations, array traversal, loop iterations, and pointer manipulation, among others. The ultimate choice between whether you wish to go for post-increment or pre-increment ultimately boils down to whether you wish to use the original value before or after the increment in the expression.
On that note, we recommend checking out upGrad’s Executive Post Graduate Program In Software Development, offered under the guidance of IIIT-B, which will help you to enhance your knowledge of C programming language. It comes alongside numerous benefits, including 1:1 high-performance coaching sessions and personalised industry sessions, among others.
1. Can you explain the precedence of pre-increment and post-increment in C?
The precedence of post-increment is significantly higher than that of pre-increment in C. Furthermore, when it comes to associativity, pre-increment has right-to-left associativity, whereas post-increment has left-to-right associativity.
2. What is the syntax for increment in C programming language?
Increment operators, whether it is post-increment or pre-increment, are denoted by the ‘++’ symbol. Their ultimate purpose is to increase the numerical count of a variable by 1.
3. What do we mean by ++I and I++ for loop?
Both these are responsible for incrementing the number; however, the ways in which they operate are different. For example, ++I increments the number before the evaluation of the current expression, whereas I++ increments the number after the evaluation of the current expression.
PAVAN VADAPALLI
Director of Engineering
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working …Read More
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. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
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 enr...