Logical and Arithmetic Operators

What Are Arithmetic Operators?

Arithmetic operators in C++ are +, -, *, / and %. Learn what each does, how integer division and remainder work, and see a working example program.

On this page
  1. The five arithmetic operators
  2. Integer division drops the fraction
  3. The remainder operator
  4. Precedence
  5. Limits and division by zero
  6. Related operators
  7. Summary

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";
}
  • x += 2 is shorthand for x = 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 of x.
  • If you have wondered whether there is a power operator in C++, the answer is no. 2 ** 3 does not compile. For powers, use std::pow from <cmath>. For example, std::pow(2, 3) returns 8.0 as a double.

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.

The complete guide to logical and arithmetic operators

The OR Operator in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

Inheritance in C++

A practical guide to inheritance in C++: the syntax, access specifiers, constructor order, virtual functions and common mistakes, with short code examples.

Inheritance 7 min read

SOLID Principles in C++

Learn the five SOLID principles in C++ with short examples of single responsibility, open/closed, Liskov, interface segregation and dependency inversion.