In C++, subtraction is performed with the arithmetic subtraction operator -. This tutorial shows how subtraction behaves with integers, floating-point values, mixed numeric types, characters, Boolean values, chained expressions, and the compound assignment operator -=.
C++ Subtraction Operator Syntax
The binary subtraction operator takes two operands. It subtracts the value on the right from the value on the left.
result = left_operand - right_operand;
For example, if a is 12 and b is 7, then a - b evaluates to 5. Operand order matters: b - a would evaluate to -5.
1. Subtract Two Integers in C++
When both operands are integers, the subtraction expression produces an integer result, provided the calculation stays within the representable range of the integer type.
int = int - int
In the following program, two int values are initialized and the second value is subtracted from the first.
C++ Program
#include <iostream>
using namespace std;
int main() {
int a = 12;
int b = 7;
int diff = a - b;
cout << diff << endl;
}
Output
5
The statement int diff = a - b; evaluates 12 - 7 and stores the result 5 in diff.
2. Subtract Floating-Point Numbers in C++
The subtraction operator also works with floating-point operands. When both operands are float, the result can be stored in a float.
float = float - float
In the following program, two floating-point values are subtracted.
C++ Program
#include <iostream>
using namespace std;
int main() {
float a = 1.8;
float b = 0.6;
float diff = a - b;
cout << diff << endl;
}
Output
1.2
Floating-point values are represented approximately in binary, so some decimal subtractions may display small rounding differences when printed with higher precision. This is normal floating-point behavior and is not specific to the subtraction operator.
3. Subtract a Float from an Integer in C++
C++ allows subtraction between different arithmetic types. When an int is combined with a float, the integer operand is converted to a compatible floating-point type for the arithmetic operation.
float = int - float
In the following program, 2 is treated as a floating-point value for the subtraction with 0.6.
C++ Program
#include <iostream>
using namespace std;
int main() {
int a = 2;
float b = 0.6;
float diff = a - b;
cout << diff << endl;
}
Output
1.4
4. Subtract Character Values in C++
Character types are integral types in C++. Before arithmetic is performed, character operands normally undergo integral promotion. Therefore, an expression such as ch1 - ch2 is generally evaluated as an int. If the result is assigned back to a char, that integer value is converted to the character type.
char = char - char
The syntax line above represents assignment of the subtraction result to a char. The subtraction expression itself is evaluated after integral promotion.
In the following program, ch1 contains 'M' and ch2 contains the numeric value 8. Their promoted integer values are subtracted, and the result is converted back to char.
C++ Program
#include <iostream>
using namespace std;
int main() {
char ch1 = 'M';
char ch2 = (char)8;
char ch = ch1 - ch2;
cout << ch << endl;
}
Output
E
This example depends on the execution character set mapping used by the implementation. For ordinary numeric differences between character codes, it is usually clearer to store the result in an int unless a character result is specifically required.
5. Subtract Boolean Values in C++
A bool is an integral type. In arithmetic expressions, false is promoted to 0 and true is promoted to 1. The subtraction expression therefore produces an integer value before any assignment conversion.
bool = bool - bool
The syntax line above shows assignment to a bool. The actual expression bool - bool is evaluated using promoted integer operands. Assigning the result to bool converts zero to false and any nonzero value to true.
In the following program, true - false becomes 1 - 0. The integer result 1 is then converted to true when stored in c.
C++ Program
#include <iostream>
using namespace std;
int main() {
bool a = false;
bool b = true;
bool c = b - a;
cout << c << endl;
}
Output
1
Although assigning the difference of two Boolean values back to bool produces false when they are equal and true when they differ, subtraction is not the usual way to express Boolean inequality. For Boolean logic, a != b communicates the intent more clearly.
Order of Operands in C++ Subtraction
Subtraction is not commutative. Reversing the operands generally changes the result.
#include <iostream>
using namespace std;
int main() {
int a = 10;
int b = 3;
cout << a - b << endl;
cout << b - a << endl;
return 0;
}
Output
7
-7
Read a - b as “subtract b from a.”
Chaining the C++ Subtraction Operator
You can chain the subtraction operator to subtract more than two operands in one expression. Subtraction is left-associative, so the expression is evaluated from left to right when only subtraction operators are involved.
result = operand_1 - operand_2 - operand_3 - ... - operand_n
Therefore, a - b - c is interpreted as (a - b) - c, not as a - (b - c).
In the following example, four integers are used in a chained subtraction expression.
C++ Program
#include <iostream>
using namespace std;
int main() {
int a = 12;
int b = 25;
int c = 61;
int d = 37;
int diff = a - b - c - d;
cout << diff << endl;
}
Output
-111
The evaluation is ((12 - 25) - 61) - 37, which gives -111.
C++ Subtraction Assignment Operator -=
The compound subtraction assignment operator -= subtracts the right operand from the variable on the left and assigns the result back to that variable.
value -= amount;
For a simple variable, value -= amount; has the same intended arithmetic effect as subtracting amount from value and storing the result back in value.
#include <iostream>
using namespace std;
int main() {
int balance = 100;
balance -= 35;
cout << balance << endl;
return 0;
}
Output
65
Unary Minus and Binary Subtraction in C++
The - symbol has two related uses. With two operands, it is the binary subtraction operator. With one operand, it is the unary minus operator, which produces the negated value of its operand.
difference = a - b;
negative_value = -a;
For example, if a is 8, then -a is -8. This is negation, not subtraction of one variable from another.
Type Conversions That Affect C++ Subtraction
Before arithmetic is performed, C++ may convert operands to a common arithmetic type. This matters when the operands have different types.
- Integer with integer: integer promotions and the usual arithmetic conversions determine the expression type.
- Integer with floating point: the integer value is converted so the arithmetic can be performed in an appropriate floating-point type.
- Character and Boolean operands: these commonly undergo integral promotion before subtraction.
- Unsigned integers: subtracting a larger unsigned value from a smaller one does not produce an ordinary negative unsigned result; unsigned arithmetic wraps modulo the range of the type.
If a negative result is meaningful in your program, choose a signed type that can represent the expected range rather than relying on unsigned subtraction.
C++ Subtraction Examples at a Glance
| Expression | Meaning | Example result |
|---|---|---|
12 - 7 | Integer subtraction | 5 |
1.8f - 0.6f | Floating-point subtraction | Approximately 1.2 |
2 - 0.6f | Mixed integer and float subtraction | Approximately 1.4 |
a -= b | Subtract b and assign back to a | Depends on operand values |
-a | Unary negation | Negative of a, when representable |
C++ Subtraction Editorial QA Checklist
- Verify that every binary subtraction example explains that the right operand is subtracted from the left operand.
- Check that mixed-type examples describe the relevant arithmetic conversion instead of implying that both operands keep their original types during evaluation.
- For
charandboolexamples, distinguish the type of the subtraction expression from the type of the variable receiving the result. - Confirm that chained subtraction is explained as left-associative, such as
(a - b) - c. - Keep
-=examples separate from plain-examples so readers can see the difference between calculating a result and updating a variable. - When unsigned values are discussed, avoid describing a wrapped result as an ordinary negative integer.
- For floating-point output, avoid promising exact decimal representation when binary floating-point rounding may be visible.
C++ Subtraction Summary
In this C++ Tutorial, we learned how to use C++ Subtraction Operator on values of different datatypes, and during different scenarios.
Use a - b to subtract b from a. Operand order matters, mixed arithmetic types can trigger conversions, chained subtraction is evaluated left to right, and -= subtracts a value while updating the left-hand variable.
TutorialKart.com