Unary Operators — Pre & Post Increment / Decrement
Last Updated: Jan 2026
Unary operators work on a single operand (one value).
Common unary operators for counters are:
- ++ → Increment by 1
- -- → Decrement by 1
These can be used in two forms:
- Prefix (pre) → ++x or --x
- Postfix (post) → x++ or x--
Hinglish Tip 🗣: Unary ka matlab ek hi value par kaam — ++ se value ek badh jaati hai, -- se ek kam.
✏ Pre-Increment / Pre-Decrement
- Pre form changes the variable first, then returns the new value.
int a = 5;
printf("%d\n",++a); // increments a to 6, then prints 6
printf("%d\n",a); // 6
int b = 5;
printf("%d\n",--b); // decrements b to 4, then prints 4
printf("%d\n",b); // 4
✏ Post-Increment / Post-Decrement
- Post form changes the variable last, then returns the old value.
int x = 5;
printf("%d\n",x++); // prints 5 (old value), then x becomes 6
printf("%d\n",x); // 6
int y = 5;
printf("%d\n",y--); // prints 5 (old value), then y becomes 4
printf("%d\n",y); // 4
Why difference matters
When used inside expressions the position affects the result:
int i = 2;
int a = i++ + 5; // a = 2 + 5 = 7, then i becomes 3
printf("%d %d\n",a, i); // 7 3
int j = 2;
int b = ++j + 5; // j becomes 3, then b = 3 + 5 = 8
printf("%d %d\n",b, j); // 8 3
⚠️ Use with care
- Avoid complex expressions with multiple increments in one line — can become confusing and output may be unexpected because of precedence or compiler optimizations.
- Prefer clear, separate statements for readability.
