Nearby lessons

32 of 124

C - Increment and Decrement Operators

Increment and Decrement Operators is one of the foundational topics in C programming. This lesson explains Increment and Decrement Operators (++ --), Program 1: pre-increment vs post-increment and Program 2: pre-decrement vs post-decrement with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Increment and Decrement Operators (++ --)

  • ++ increases a variable by 1 (increment).
  • decreases a variable by 1 (decrement).
  • Pre form (++a) — increase first, then use the value.
  • Post form (a++) — use the value first, then increase.
Trainer's Note: Memory trick: pre = prefix, so it acts first. post = after, so it acts last. Read a++ as "use a, then add 1" and ++a as "add 1, then use a". The two programs below show exactly this.

Program 1: pre-increment vs post-increment

Example02
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int a = 10;
6 
7 printf("a is %d\n", a); // 10
8 printf("a++ prints %d\n", a++); // 10 (print first, then add)
9 printf("now a is %d\n", a); // 11
10 printf("++a prints %d\n", ++a); // 12 (add first, then print)
11}
Output
a is 10 a++ prints 10 now a is 11 ++a prints 12

Program 2: pre-decrement vs post-decrement

Example03
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int a = 10;
6 
7 printf("a-- prints %d\n", a--); // 10 (print first, then subtract)
8 printf("now a is %d\n", a); // 9
9 printf("--a prints %d\n", --a); // 8 (subtract first, then print)
10}
Output
a-- prints 10 now a is 9 --a prints 8
📝 Key Takeaways
  • Operators: arithmetic (+ - * / %), relational (== != < > <= >=), logical (&& || !), assignment (= += -= *= /= %=), ++ --, ternary ?:.
  • Each operator family has its own complete program — copy and run one at a time.
  • Integer division drops decimals: 5/2 = 2; use 5.0/2 for 2.5.
  • % gives the remainder and works only on integers.
  • a++ uses then increases; ++a increases then uses.
  • Ternary: condition ? value1 : value2.
  • Use brackets to control the order of evaluation.

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1