Arithmetic operators do math on numbers. C++ has five that take two operands: +, -, *, / and %.
The five arithmetic operators
+adds two values.-subtracts the right operand from the left.*multiplies two values./divides the left operand by the right.%gives the remainder after dividing the left operand by the right.
This program uses all five:
#include <iostream>
int main() {
int a = 7;
int b = 2;
std::cout << "a + b = " << a + b << "\n"; // 9
std::cout << "a - b = " << a - b << "\n"; // 5
std::cout << "a * b = " << a * b << "\n"; // 14
std::cout << "a / b = " << a / b << "\n"; // 3
std::cout << "a % b = " << a % b << "\n"; // 1
double x = 7.0;
std::cout << "x / b = " << x / b << "\n"; // 3.5
return 0;
}
Output:
a + b = 9
a - b = 5
a * b = 14
a / b = 3
a % b = 1
x / b = 3.5
Integer division drops the fraction
When both operands are int, / gives an int result. The fraction is dropped, so 7 / 2 is 3, not 3.5. Since C++11, the result is rounded toward zero, so -7 / 2 is -3.
To get a fraction, make at least one operand a floating-point type. You can write 7.0 or convert one operand with static_cast<double>:
int count = 5;
double average = count / 2.0; // 2.5
double half = static_cast<double>(count) / 2; // 2.5
// count / 2 would give 2, because both operands are int
The remainder operator
% gives what is left over after integer division. 7 % 2 is 1, because 2 fits into 7 three times with 1 left over. The % operator works only on integer types. Using it on a double does not compile.
Since C++11, the result takes the sign of the left operand, so -7 % 2 is -1.
A common use is checking whether a number is even:
if (n % 2 == 0) {
std::cout << n << " is even\n";
}
Precedence
*, / and % run before + and -:
int a = 2 + 3 * 4; // 14, not 20
Operators at the same level run from left to right. So 8 / 4 / 2 is (8 / 4) / 2, which is 1. Parentheses make the order clear: (2 + 3) * 4 is 20.
Limits and division by zero
A result must fit in the type you use. For int, signed overflow is undefined behavior in C++, so you cannot rely on the value wrapping around. If numbers can get large, check the limits in <climits>, such as INT_MAX, or use a wider type such as long long.
Dividing an integer by zero with / or % is also undefined behavior. Check the divisor first:
if (b != 0) {
std::cout << a / b << "\n";
}
Related operators
x += 2is shorthand forx = x + 2. The Assignment Operators in C++ article covers that family.++and--add or subtract one. They are covered in the increment operator in C++.- The
-sign can also work on one operand, as in-x, which gives the negative ofx. - If you have wondered whether there is a power operator in C++, the answer is no.
2 ** 3does not compile. For powers, usestd::powfrom<cmath>. For example,std::pow(2, 3)returns8.0as adouble.
Summary
Five operators do the core math: +, -, *, / and %. Dividing two int values drops the fraction, the remainder operator needs integers, and multiplication and division come before addition and subtraction. For the full picture of operator groups, read what are operators in c with examples.