Increment and decrement operators help you add or subtract 1 from a number quickly and simply.
Increment and decrement operators
Introduction
Syntax
C
variable++; variable--; ++variable; --variable;
variable++ adds 1 after using the variable's current value.
++variable adds 1 before using the variable's value.
Examples
C
int x = 5; x++; // x becomes 6
C
int y = 5; ++y; // y becomes 6
C
int z = 5; z--; // z becomes 4
C
int w = 5; --w; // w becomes 4
Sample Program
This program shows how the count variable changes when incremented and decremented using both postfix and prefix operators.
C
#include <stdio.h> int main() { int count = 10; printf("Initial count: %d\n", count); count++; printf("After count++: %d\n", count); ++count; printf("After ++count: %d\n", count); count--; printf("After count--: %d\n", count); --count; printf("After --count: %d\n", count); return 0; }
Important Notes
Postfix (variable++) uses the value first, then changes it.
Prefix (++variable) changes the value first, then uses it.
Use these operators to write shorter and clearer code when adding or subtracting 1.
Summary
Increment (++) adds 1; decrement (--) subtracts 1.
Postfix form changes value after use; prefix form changes before use.
They are useful for counting and looping tasks.
Practice
1. What does the
++ operator do in C?easy
Solution
Step 1: Understand the meaning of
The++++operator increases the value of a variable by 1.Step 2: Compare with other options
Subtracting, multiplying, or dividing are not what++does.Final Answer:
Adds 1 to the variable -> Option AQuick Check:
++means add 1 [OK]
Hint: Remember ++ means add one, -- means subtract one [OK]
Common Mistakes:
- Confusing ++ with --
- Thinking ++ multiplies
- Mixing increment with assignment
2. Which of the following is the correct syntax for prefix decrement of a variable
x by 1 in C?easy
Solution
Step 1: Identify prefix decrement syntax
The prefix decrement operator is--x;, which decrements x by 1.Step 2: Check options carefully
A is postfixx--;, Bx-+;and Cx+-;invalid syntax, D is correct prefix.Final Answer:
--x; -> Option DQuick Check:
Prefix decrement--x;is correct syntax [OK]
Hint: Look for correct -- syntax before or after variable [OK]
Common Mistakes:
- Using invalid operators like -+ or +-
- Confusing prefix and postfix syntax
- Missing semicolon
3. What is the output of this C code?
int x = 5;
printf("%d", x++);
printf(" %d", x);medium
Solution
Step 1: Understand postfix increment
Postfix means use the current value first, then add 1. Sox++printf("%d", x++)prints 5, then x becomes 6.Step 2: Print updated value of x
The secondprintfprints the new value of x, which is 6.Final Answer:
5 6 -> Option BQuick Check:
Postfix prints old value, then increments [OK]
Hint: Postfix ++ prints old value, prefix ++ prints new value [OK]
Common Mistakes:
- Confusing postfix with prefix
- Expecting increment before print
- Ignoring space in output
4. Find the error in this code snippet:
int a = 3;
int b = ++a++;
printf("%d", b);medium
Solution
Step 1: Analyze the expression
This expression tries to apply prefix and postfix increment operators together on the same variable, which is invalid syntax in C.++a++Step 2: Understand compiler rules
C does not allow chaining increments like this; it causes a syntax error.Final Answer:
Syntax error due to invalid use of increment operators -> Option AQuick Check:
Invalid chaining of ++ operators causes syntax error [OK]
Hint: Don't chain prefix and postfix ++ on same variable [OK]
Common Mistakes:
- Thinking it increments twice
- Ignoring syntax error
- Assuming valid output
5. Consider this code:
int arr[3] = {1, 2, 3};
int i = 0;
int sum = 0;
while (i < 3) {
sum += arr[i++];
}
printf("%d", sum);
What is the output and why?hard
Solution
Step 1: Understand postfix increment in
The value of i is used first to access the array element, then i is incremented.arr[i++]Step 2: Calculate sum step-by-step
Elements accessed: arr[0]=1, arr[1]=2, arr[2]=3; sum = 1+2+3 = 6.Final Answer:
6 because i increments after accessing each element -> Option CQuick Check:
Postfix increments after use, sum is 6 [OK]
Hint: Postfix increments after use, so sum all elements correctly [OK]
Common Mistakes:
- Confusing prefix and postfix increments
- Calculating wrong sum
- Assuming i increments before access
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
