In this C++ tutorial, you will learn how to use Arithmetic Subtraction Operator with values of different datatypes using example programs.
C++ Subtraction
In C++, Subtraction is performed using arithmetic operator -
. The operator takes two operands and returns the subtraction of second operand from first operand.
1. Subtraction of Integers
You can subtract two integers using subtraction operator. The datatype of the operands and returned value is given in the following code snippet.
int = int - int
In the following program, we initialize two integer variables and subtract second value from first value using subtraction operator.
C++ Program
#include <iostream>
using namespace std;
int main() {
int a = 12;
int b = 7;
int diff = a - b;
cout << diff << endl;
}
Output
5
2. Subtraction of Floating Point Numbers
You can subtract two floating point numbers using subtraction operator. The datatype of the operands and returned value is given in the following code snippet.
float = float - float
In the following program, we initialize two floating point variables and find the difference using subtraction operator.
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
3. Subtraction of Floating Point Number from Integer
You can subtract an integer and floating point number using subtraction operator. The datatype of the operands and returned value is given in the following code snippet.
float = int - float
In the following program, we initialize an integer variable and a floating point variable and compute their difference using subtraction operator.
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. Subtraction of Chars
You can subtract two characters using subtraction operator. The datatype of the operands and returned value is given in the following code snippet.
char = char - char
In the following program, we initialize two character variables and subtract them using subtraction operator.
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
5. Subtraction of Boolean values
You can find the difference of two boolean values using subtraction operator. The operator converts false to 0, true to 1 and then performs subtraction operation. The datatype of the operands and returned value is given in the following code snippet.
bool = bool - bool
The subtraction operation boolean values is equivalent to XOR operation of boolean values. Meaning, the difference betwee true and false returns a true, while the difference between two true or two false returns a false.
In the following program, we initialize two character variables and add them using addition operator.
C++ Program
#include <iostream>
using namespace std;
int main() {
bool a = false;
bool b = true;
bool c = b - a;
cout << c << endl;
}
Output
1
Chaining of Subtraction Operator
You can chain Subtraction Operator and perform the subtraction of more than two operands in a single statement. The sudo code is given below.
result = operand_1 - operand_2 - operand_3 - ... - operand_n
In the following example, we shall take four integer variables and subtract the rest of operands from first operand in a single statement using arithmetic subtraction operator.
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
Conclusion
In this C++ Tutorial, we learned how to use C++ Subtraction Operator on values of different datatypes, and during different scenarios.