CppArithmetic Operators

Arithmetic Operators

C++ provides five basic arithmetic operators: + (addition),{' '} - (subtraction), * (multiplication),{' '} / (division), and % (modulo, the remainder of integer division).

CPP
#include <iostream>

int main() {
    int a = 10, b = 3;

    std::cout << a + b << std::endl; // 13
    std::cout << a - b << std::endl; // 7
    std::cout << a * b << std::endl; // 30
    std::cout << a / b << std::endl; // 3  (integer division truncates)
    std::cout << a % b << std::endl; // 1  (remainder)
    return 0;
}
Integer Division Truncates
7 / 2 is 3, not 3.5
When both operands of / are integers, C++ performs integer division: the result is truncated toward zero, discarding any fractional part. This surprises many beginners coming from languages where division always produces a floating-point result. To get a fractional result, at least one operand must be a floating-point type.

CPP
int a = 7, b = 2;
std::cout << a / b << std::endl;             // 3 (integer division)
std::cout << (double)a / b << std::endl;      // 3.5 (one operand cast to double)
std::cout << 7.0 / 2 << std::endl;            // 3.5 (a literal 7.0 forces double)
Modulo Only Works on Integers
% is only defined for integer operand types in standard C++. To compute a remainder for floating-point values, use{' '} std::fmod from <cmath>.

CPP
#include <cmath>

int wholeRemainder = 10 % 3;        // 1 — fine, both operands are int
// double bad = 10.5 % 3;           // ERROR: % does not accept doubles
double floatRemainder = std::fmod(10.5, 3.0); // 1.5 — use fmod instead
Increment and Decrement: Prefix vs Postfix
Both ++ and -- come in two forms that differ in *when* the updated value becomes visible.

Form

Syntax

Behavior

Prefix

++x

Increments x first, then evaluates to the new value

Postfix

x++

Evaluates to the original value first, then increments x

CPP
int x = 5;
int prefixResult = ++x;   // x becomes 6, prefixResult is 6

int y = 5;
int postfixResult = y++;  // postfixResult is 5, y becomes 6 (after this line)

std::cout << x << " " << prefixResult << std::endl; // 6 6
std::cout << y << " " << postfixResult << std::endl; // 6 5
Avoid mixing increment/decrement with other uses of the same variable
Expressions like x = x++ + ++x; have unspecified or undefined evaluation order in various C++ standard versions and should never be written — the result differs across compilers. Keep increment/decrement as their own statement whenever the expression gets non-trivial.
Operator Precedence Teaser
Arithmetic operators follow the familiar mathematical precedence:{' '} *, /, and % bind tighter than{' '} + and -. The full precedence table across *all* operator categories (including bitwise, relational, and assignment) is covered on the dedicated **Operator Precedence** page.
  • 2 + 3 * 4 evaluates to 14, not 20, because * runs before +

  • Use parentheses (2 + 3) * 4 to force a different grouping — it also improves readability even when not strictly required

Note
Chained arithmetic on mixed types (e.g. int and double) promotes the integer operand to match the floating-point one before the operation runs, following the usual arithmetic conversion rules.