Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Operators

C++ arithmetic operators

Reference for C++ arithmetic operators +, -, *, / and %: integer division, remainder signs, promotions, overflow, floating-point rules and std::div.

The arithmetic operators are unary + and -, and the binary +, -, *, / and %. They work on integer and floating-point types (% on integers only). Before the operation, operands go through integral promotion and the usual arithmetic conversions, so the type of the result may differ from the types you wrote.

Two rules cause most bugs: integer division discards the fractional part, and signed overflow is undefined behavior.

C++
int a = 7, b = 2;
int q = a / b;     // 3
int r = a % b;     // 1
double d = a / 2.0; // 3.5

Try it

C++ Division and remainder

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Promotions, floating point and unsigned wrap-around

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
+a          -a
a + b       a - b       a * b       a / b       a % b
Name Description
+a, -a Unary plus and minus. Both promote small integer types to int; unary - on an unsigned value wraps around.
a + b, a - b, a * b Addition, subtraction, multiplication.
a / b Division. On integers the result is truncated toward zero.
a % b Remainder, integers only. The sign of the result follows a.

Pointers also support p + n, p - n and p - q (see Pointers).

Returns: a prvalue of the common type of the operands (after promotions).

Errors / undefined behavior: integer division or % by zero is undefined behavior; so is INT_MIN / -1. Signed overflow (INT_MAX + 1) is undefined behavior, while unsigned arithmetic wraps modulo 2^N. Floating-point division by zero gives infinity or NaN on IEEE 754 platforms.

Usage notes

  • Integer division: convert one operand to a floating type before dividing (double(a) / b) if you want a fractional result. Writing double r = a / b; divides as integers first.
  • Remainder sign: since C++11, a / b truncates toward zero and a % b == a - (a / b) * b. Use the formula in the first example for a non-negative modulo.
  • Mixed signedness: comparing or combining negative int with unsigned values is a classic bug. Prefer std::ssize() and signed indices, or std::cmp_less for safe comparisons.
  • Overflow checks: test before the operation (a > max - b), use a wider type, or use compiler builtins such as __builtin_add_overflow on GCC and Clang. C++26 adds saturating arithmetic (std::add_sat and friends) in <numeric>.
  • Floating-point %: use std::fmod. Results of floating-point math may differ slightly between platforms and optimization settings.
  • Operator overloading: you can overload all of these for your own types. Implement += as a member and + as a free function on top of it (Operator overloading).
  • Evaluation order: the operands of a + b are unsequenced relative to each other: do not write f() + g() if their order matters.