Understanding the --
Decrement Operator in C++
The --
operator in C++ is a unary operator used to decrease a variable’s value by 1. Like the increment operator (++
), the decrement operator also has two forms:
- Prefix:
--x
- Postfix:
x--
Syntax
--x; // Prefix decrement
x--; // Postfix decrement
Prefix vs Postfix
Form | Description | Returns |
---|---|---|
--x | Decrements first, then returns new value | New value |
x-- | Returns current value, then decrements | Original value |
Example with Integers
decrement_integers.cpp
#include <iostream>
int main() {
int a = 5;
std::cout << "Prefix: " << --a << std::endl; // Output: 4
std::cout << "Postfix: " << a-- << std::endl; // Output: 4
std::cout << "Now a = " << a << std::endl; // Output: 3
return 0;
}
Explanation:
--a
decreasesa
before it is printed. So--a
results in4
.a--
prints the current value (4
) then decreases it to3
.- By the end,
a
becomes3
.
Example in a Loop (Countdown)
decrement_loop.cpp
#include <iostream>
int main() {
for (int i = 3; i > 0; --i) {
std::cout << "Countdown: " << i << std::endl;
}
return 0;
}
Explanation:
- The loop starts from
3
and decrementsi
each time. --i
ensures the decrement happens before the next condition check.
Example with char
Type
decrement_char.cpp
#include <iostream>
int main() {
char ch = 'D';
--ch;
std::cout << "ch = " << ch << std::endl; // Output: C
return 0;
}
Explanation:
- Characters in C++ are represented by ASCII codes.
'D'
has ASCII code 68, so--ch
makes it 67 which is'C'
.
Example with Floating Point Numbers
decrement_float.cpp
#include <iostream>
int main() {
float f = 5.5f;
f--;
std::cout << "f = " << f << std::endl; // Output: 4.5
return 0;
}
Explanation:
f--
subtracts1.0
from the float value.- This is valid and works the same as with integers.
Example with Pointers
decrement_pointer.cpp
#include <iostream>
int main() {
int arr[] = {10, 20, 30};
int* ptr = &arr[2]; // Points to 30
std::cout << *ptr << std::endl; // Output: 30
std::cout << *(--ptr) << std::endl; // Output: 20
return 0;
}
Explanation:
--ptr
moves the pointer back to the previous element in the array.- First it prints 30, then moves to 20 using
--ptr
.
Example with Arrays
decrement_array.cpp
#include <iostream>
int main() {
int scores[3] = {90, 85, 80};
for (int i = 0; i < 3; i++) {
scores[i]--;
std::cout << scores[i] << " ";
}
return 0;
}
Output:
89 84 79
Explanation:
- Each array element is decremented by 1 using
scores[i]--
. - The updated values are printed after modification.
Example with Custom Class (Operator Overloading)
decrement_overload.cpp
#include <iostream>
class Counter {
int count;
public:
Counter(int c = 0) : count(c) {}
Counter& operator--() { // Prefix
--count;
return *this;
}
Counter operator--(int) { // Postfix
Counter temp = *this;
count--;
return temp;
}
void show() const { std::cout << "Count = " << count << std::endl; }
};
int main() {
Counter c(5);
--c;
c.show(); // Output: 4
c--;
c.show(); // Output: 3
return 0;
}
Explanation:
- Prefix version directly decrements the count.
- Postfix version keeps the original, then decrements.
- Useful when creating user-defined counters or custom iterators.
Invalid Use Cases
decrement_invalid.cpp
// double x = 1.0;
// --(x + 1.0); // ❌ Invalid: x + 1.0 is a temporary (r-value)
Explanation:
- You cannot decrement an r-value (temporary expression).
- Only variables (l-values) can be modified in place.
Summary Table
Context | Works? | Example | Notes |
---|---|---|---|
int | Yes | --x , x-- | Most common use |
char | Yes | --ch | Works with ASCII |
float/double | Yes | --f | Decrements by 1.0 |
pointer | Yes | --ptr | Moves pointer to previous element |
array | Yes | arr[i]-- | Modifies element by 1 |
Custom Class | Yes | Overload -- | Define prefix/postfix behavior |
r-value | No | --(x+1) | ❌ Cannot modify temporary value |
Use Cases
- Countdown timers
- Reversing loops
- Pointer navigation (moving backwards)
- Custom iterator types
- Simple calculations and updates in control flow
Conclusion
The --
decrement operator is simple yet powerful when used correctly. By understanding the difference between prefix and postfix, and how it behaves with different types (including pointers and custom classes), you can write cleaner and more expressive C++ code. It’s widely used in loops, data manipulation, and pointer arithmetic.