C++Increment and DecrementDecrement Operator

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

FormDescriptionReturns
--xDecrements first, then returns new valueNew value
x--Returns current value, then decrementsOriginal 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 decreases a before it is printed. So --a results in 4.
  • a-- prints the current value (4) then decreases it to 3.
  • By the end, a becomes 3.

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 decrements i 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-- subtracts 1.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

ContextWorks?ExampleNotes
intYes--x, x--Most common use
charYes--chWorks with ASCII
float/doubleYes--fDecrements by 1.0
pointerYes--ptrMoves pointer to previous element
arrayYesarr[i]--Modifies element by 1
Custom ClassYesOverload --Define prefix/postfix behavior
r-valueNo--(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.