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.
int a = 7, b = 2; int q = a / b; // 3 int r = a % b; // 1 double d = a / 2.0; // 3.5
Try it
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.
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
+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. Writingdouble r = a / b;divides as integers first. - Remainder sign: since C++11,
a / btruncates toward zero anda % b == a - (a / b) * b. Use the formula in the first example for a non-negative modulo. - Mixed signedness: comparing or combining negative
intwith unsigned values is a classic bug. Preferstd::ssize()and signed indices, orstd::cmp_lessfor safe comparisons. - Overflow checks: test before the operation (
a > max - b), use a wider type, or use compiler builtins such as__builtin_add_overflowon GCC and Clang. C++26 adds saturating arithmetic (std::add_satand friends) in<numeric>. - Floating-point
%: usestd::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 + bare unsequenced relative to each other: do not writef() + g()if their order matters.