What are Increment and Decrement Operators in C?
In C, increment and decrement operators are used to increase or decrease the value of a variable by 1. These are unary operators, meaning they operate on a single operand.
Syntax of Increment and Decrement Operators
// Increment variable++; // Post-increment ++variable; // Pre-increment // Decrement variable--; // Post-decrement --variable; // Pre-decrement
Pre-Increment vs Post-Increment
Operator | Description | Usage Example |
---|---|---|
++i | Pre-increment | Increments value before use |
i++ | Post-increment | Increments value after use |
–i | Pre-decrement | Decreases value before use |
i– | Post-decrement | Decreases value after use |
Examples
1. Prefix Increment (++a)
int a = 5; int b = ++a; // a becomes 6, then b is assigned 6 printf("%d %d", a, b); // Output: 6 6
2. Postfix Increment (a++)
int a = 5; int b = a++; // b is assigned 5, then a becomes 6 printf("%d %d", a, b); // Output: 6 5
3. Prefix Decrement (–a)
int a = 5; int b = --a; // a becomes 4, then b is assigned 4 printf("%d %d", a, b); // Output: 4 4
4. Postfix Decrement (a–)
int a = 5; int b = a--; // b is assigned 5, then a becomes 4 printf("%d %d", a, b); // Output: 4 5
Example: Pre-Increment vs Post-Increment
#include <stdio.h> int main() { int a = 5; printf("Pre-Increment: %d\n", ++a); // Output: 6 printf("Post-Increment: %d\n", a++); // Output: 6 (then a becomes 7) printf("Now a is: %d\n", a); // Output: 7 return 0; }
Increment/Decrement in Loops
These operators are frequently used in for, while, and do-while loops.
For Loop with Increment
for(int i = 0; i < 5; i++) { printf("%d ", i); }
While Loop with Decrement
int i = 5; while(i > 0) { printf("%d ", i); i--; }
Advantages and Disadvantages
Advantages of Increment and Decrement Operators in C
- Instead of writing something like i = i + 1, you can just write i++. It saves time and makes your code look neat.
- These operators are super handy when you’re working with loops. In for loops especially, i++ or i– makes the loop logic simple and easy to follow.
- In straightforward code, using ++ and — actually improves readability. It’s much easier to glance at count++ than count = count + 1.
- Since these operations are supported directly by most computer processors, they’re usually faster than regular arithmetic. That’s great for performance-critical applications.
- You can use them right inside other statements, like in conditions or assignments. It helps reduce the number of lines and keeps logic compact.
Disadvantages of Increment and Decrement Operators in C
- If you start combining ++ or — with other operators in a big expression, it gets confusing fast. It’s easy to lose track of what’s happening.
- One small error, like using i++ instead of ++i, can lead to wrong results. These kinds of logic errors can be tough to spot.
- Using the same variable multiple times with ++ or — in one statement can confuse the compiler and cause unexpected results. This is risky and should be avoided.
- You can’t use these operators with things like structs or strings in C. They’re only meant for numbers—like int, float, or char.
- Because these operators can change a variable’s value as part of an expression, it’s sometimes hard to trace what went wrong when debugging.
Precedence of ++ and — operators
The increment (++) and decrement (–) operators come in two forms:
- Prefix: ++i, –i
- Postfix: i++, i–
Operator Precedence (Order of Evaluation)
Operator Type | Symbol | Category | Precedence Level | Associativity |
---|---|---|---|---|
Postfix | i++, i– | Highest (among unary operators) | Very High | Left to Right |
Prefix | ++i, –i | Unary Operators | High | Right to Left |
- Postfix (i++, i–) has higher precedence than prefix (++i, –i).
- That means if both appear in the same expression (which is rare and discouraged), postfix will be evaluated first.
For example:
int a = 5; int b = a++ + ++a;
Here, a++ is evaluated before ++a.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Practice BCA MCQs
- Apply for C Internship
- Apply for Computer Science Internship
- Check Computer Science Books
- Watch Advanced C Programming Videos