In this C++ tutorial, you will learn how to decrement a variable by 1 using the decrement operator --, how prefix and postfix decrement differ, and how to decrease a value by more than 1 when needed.

C++ Decrement Operator --

The C++ decrement operator is written as --. It is a unary operator, which means it works with one operand. For a numeric variable, it decreases the stored value by exactly 1.

For example, if a contains 12, either --a or a-- changes the value of a to 11. The difference between the two forms matters when the decrement expression is used as part of another expression.

C++ provides two forms of the decrement operator:

  • Prefix decrement: --a
  • Postfix decrement: a--
FormSyntaxValue used by the expressionValue stored afterward
Prefix decrement--aThe decremented valuea - 1
Postfix decrementa--The original valuea - 1

If the decrement is used as a standalone statement, such as --a; or a--;, both forms simply reduce a by 1. Prefix versus postfix becomes important when the value produced by the expression is immediately used, assigned, or printed.

Prefix Decrement in C++: --variable

In prefix decrement, the -- operator appears before the operand. The variable is decremented first, and the expression uses the new value.

</>
Copy
--operand

C++ Prefix Decrement Example

In the following example, a starts at 12. The statement --a; decreases it to 11 before the next statement prints the value.

C++ Program

</>
Copy
#include <iostream>  
using namespace std;

int main() {
   int a = 12;
   --a;
   cout << a << endl;
}

Output

11

Here, the prefix form is used as a standalone statement, so its effect is straightforward: 12 becomes 11.

Postfix Decrement in C++: variable--

In postfix decrement, the -- operator appears after the operand. The expression first produces the variable’s current value, and the variable is then decremented.

</>
Copy
operand--

C++ Postfix Decrement Example

In the following example, a starts at 12. The statement a--; decreases the stored value to 11, which is then printed.

C++ Program

</>
Copy
#include <iostream>  
using namespace std;

int main() {
   int a = 12;
   a--;
   cout << a << endl;
}

Output

11

Because a-- is a complete statement here, there is no visible difference from --a. The distinction appears when the decrement expression’s value is used immediately.

Prefix vs Postfix Decrement When the Value Is Used

The following program shows the key difference between postfix and prefix decrement. Both variables begin at 12, but the value printed by the decrement expression is different.

Printing Postfix and Prefix Decrement Results

In the following example, we shall perform postfix and prefix decrement on different variables, and then look into how they act during execution.

C++ Program

</>
Copy
#include <iostream>  
using namespace std;

int main() {
   //postfix
   int a = 12;
   cout << a-- << endl; //12
   cout << a << endl << endl; //11

   //prefix
   int b = 12;
   cout << --b << endl; //11
   cout << b << endl; //11
}

Output

12
11

11
11

For cout << a-- << endl;, postfix decrement produces the old value, so 12 is printed. The stored value of a then becomes 11, which is why the next statement prints 11.

For cout << --b << endl;, prefix decrement changes b from 12 to 11 first. The expression therefore produces 11, and b remains 11 afterward.

Assignment with Prefix and Postfix Decrement

The same rule is easier to see when the expression result is assigned to another variable. With prefix decrement, the assigned value is the new value. With postfix decrement, the assigned value is the old value.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int a = 10;
    int x = --a;

    int b = 10;
    int y = b--;

    cout << "a = " << a << ", x = " << x << '\n';
    cout << "b = " << b << ", y = " << y << '\n';
}

Output

a = 9, x = 9
b = 9, y = 10

After both operations, the original variables a and b contain 9. Only the value produced during the expression differs.

Difference Between Increment and Decrement Operators in C++

The increment and decrement operators are paired unary operators. Increment uses ++ to increase a modifiable value by 1, while decrement uses -- to decrease it by 1.

OperationPrefix formPostfix formEffect on numeric variable
Increment++aa++Adds 1
Decrement--aa--Subtracts 1

Prefix and postfix have the same general distinction for both operators: the prefix form uses the modified value, while the postfix form produces the original value before the modification.

Decrement a Floating-Point Value in C++

The decrement operator also works with floating-point variables. It subtracts 1 from the stored value, so decrementing 12.5 produces 11.5.

C++ Float Decrement Example

In the following example, we shall initialize a float variable with some value and decrement its value by 1 using -- operator.

C++ Program

</>
Copy
#include <iostream>  
using namespace std;

int main() {
   float a = 12.5;
   a--;
   cout << a << endl;
}

Output

11.5

Decrement by 2 or Another Amount in C++

The -- operator always decrements by exactly 1. There is no special decrement operator for subtracting 2, 5, or another amount. Use the subtraction-assignment operator -= instead.

</>
Copy
variable -= amount;

For example, count -= 2; subtracts 2 from count.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int count = 10;
    count -= 2;
    cout << count << '\n';
}

Output

8

Using the C++ Decrement Operator in a Countdown Loop

A common use of decrement is counting downward in a loop. In a for loop, the update expression can use i-- to reduce the loop counter by 1 after each iteration.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    for (int i = 5; i >= 1; i--) {
        cout << i << ' ';
    }
}

Output

5 4 3 2 1

Here, i-- runs after the loop body on each iteration. Since the value produced by the postfix expression is not otherwise used, the practical purpose is simply to decrease i by 1.

Valid Operands for the Built-in C++ Decrement Operator

For the built-in decrement operator, the operand must be something that can actually be modified. Numeric variables such as integers and floating-point variables can be decremented. Pointers to objects can also support decrement, which moves the pointer to the previous element according to pointer arithmetic.

A literal such as 10 cannot be decremented, because it is not a modifiable object. A const variable also cannot be changed. In current C++ standards, the built-in decrement operator is not valid for bool.

</>
Copy
int a = 10;
--a;             // valid

const int b = 10;
// --b;          // invalid: b is const

// --10;         // invalid: 10 is not a modifiable variable

Common C++ Decrement Mistakes

  • Expecting -- to subtract more than 1: use -= amount when the step is larger than 1.
  • Assuming postfix returns the new value: a-- produces the old value for the surrounding expression, even though a is decremented.
  • Trying to decrement a constant or literal: the operand must be modifiable.
  • Putting several modifications of the same variable into one complicated expression: keep decrement operations separate unless you understand C++ sequencing rules for that expression.

When learning the operator, simple statements such as --count;, count--;, or count -= 2; make the intended state change clear.

C++ Decrement Operator Summary

  • --a is prefix decrement: decrement first, then use the new value.
  • a-- is postfix decrement: produce the old value, then decrement the variable.
  • When used as standalone statements, both forms reduce the variable by 1.
  • The -- operator always changes a numeric value by 1; use -= to subtract a different amount.
  • Decrement is commonly used in countdown loops and other situations where a value must move downward one step at a time.

In this C++ Tutorial, we learned what decrement operator does, how to use decrement operator in prefix or postfix form, with the help of example C++ programs.