01 / 21
+ * / %

Arithmetic Operators
& Precedence

Master C++ arithmetic operators, understand operator precedence, and learn how expressions are evaluated step by step.

C++

What Are Arithmetic Operators?

Arithmetic operators are symbols that tell the compiler to perform mathematical calculations on numeric values. They are the building blocks of every computation in C++: from simple counters to complex physics simulations.

Binary
Operate on two operands, like \( a + b \)
Unary
Operate on one operand, like \( -a \)

The Five Binary Operators

+ Addition
\( 7 + 3 = 10 \)
- Subtraction
\( 7 - 3 = 4 \)
* Multiplication
\( 7 \times 3 = 21 \)
/ Division
\( 7 / 3 = 2 \) (int)
% Modulus
\( 7 % 3 = 1 \)

Integer vs. Floating-Point Division

The behavior of the / operator depends on the types of its operands. This is one of the most important distinctions in C++.

int a = 7, b = 2; double x = 7.0, y = 2.0; cout << a / b; // Output: 3 (truncated) cout << x / y; // Output: 3.5 (exact) cout << a / y; // Output: 3.5 (promoted)

The Modulus Operator %

The modulus operator returns the remainder after integer division. It works only with integers in C++.

\( 17 \div 5 \) visualized group 1 group 2 group 3 remainder \( 17 / 5 = 3 \) \( 17 % 5 = 2 \)

What Is Operator Precedence?

When an expression contains multiple operators, C++ must decide which operation to perform first. Precedence defines this order, just like the mathematical order of operations.

3 + 4 * 2 evaluated first: \( 4 \times 2 = 8 \) = 11 then: \( 3 + 8 = 11 \)

The Precedence Hierarchy

1 Parentheses ( ) Highest 2 Unary +, Unary - +x -x 3 Multiply, Divide, Mod * / % 4 Add, Subtract + - Lowest PRIORITY

Left-to-Right Associativity

When two operators have equal precedence, C++ resolves ties using associativity. All binary arithmetic operators associate left to right.

// 20 / 4 / 2 — which division first? int result = 20 / 4 / 2; // Step 1: (20 / 4) = 5 // Step 2: (5 / 2) = 2 // result = 2

Parentheses Override Everything

Parentheses have the highest precedence. Anything inside ( ) is evaluated first, regardless of other rules.

Without Parentheses
\( 3 + 4 \times 2 \)
\( = 3 + 8 = \) 11
With Parentheses
\( (3 + 4) \times 2 \)
\( = 7 \times 2 = \) 14

Evaluating Expressions: Step by Step

1
Scan for parentheses. Evaluate the innermost parenthesized sub-expressions first.
2
Apply unary operators. Resolve any unary + or - signs next.
3
Multiply, divide, modulus. Evaluate *, /, % from left to right.
4
Add and subtract. Evaluate + and - from left to right.
5
Check your types. Remember integer vs. floating-point division at each step.

Example 1: Simple Expression

Evaluate: \( 2 + 3 \times 4 - 1 \)

Step 1 2 + 3 * 4 - 1 = 12 Step 2 2 + 12 - 1 = 14 Step 3 14 - 1 = 13 Result: 13

Example 2: Parentheses Change the Result

Evaluate: \( (2 + 3) \times (4 - 1) \)

Step 1a (2 + 3) * (4 - 1) = 5 Step 1b 5 * (4 - 1) = 3 Step 2 5 * 3 = 15 Result: 15

Example 3: Division & Modulus

Evaluate with int types: \( 17 / 3 + 17 % 3 \times 2 \)

Step 1 17 / 3 + 17 % 3 * 2 = 5 Step 2 5 + 17 % 3 * 2 = 2 Step 3 5 + 2 * 2 = 4 Step 4 5 + 4 = 9 Result: 9

Example 4: Nested Parentheses

Evaluate: \( ((8 - 2) \times (3 + 1)) / 4 \)

// Inner parentheses first: ((8 - 2) * (3 + 1)) / 4 // Step 1: (8 - 2) = 6 // Step 2: (3 + 1) = 4 // Step 3: (6 * 4) = 24 // Step 4: 24 / 4 = 6

Common Mistakes

✕ Forgetting Integer Division
Writing \( 5 / 2 \) and expecting \( 2.5 \). With int types, the result is \( 2 \). Use \( 5.0 / 2 \) to get 2.5.
✕ Ignoring Precedence
Assuming \( a + b * c \) means \( (a + b) * c \). Multiplication happens first unless you add parentheses.
✕ Division by Zero
Using \( x / 0 \) or \( x % 0 \) causes undefined behavior at runtime. Always validate your divisor.

Operator Summary Table

Operator Meaning Precedence Example
( ) Grouping Highest \( (3 + 2) \)
+ - (unary) Sign 2nd \( -x \)
* Multiply 3rd \( 4 \times 3 = 12 \)
/ Divide 3rd \( 7 / 2 = 3 \)
% Modulus 3rd \( 7 % 2 = 1 \)
+ Add 4th \( 3 + 5 = 8 \)
- Subtract 4th \( 9 - 4 = 5 \)

Question 1 of 5

What is the value of the expression \( 10 + 6 / 3 \) in C++ (using int types)?

Question 2 of 5

What does \( 15 % 4 \) evaluate to in C++?

Question 3 of 5

What is the result of \( 7 / 2 \) when both operands are int?

Question 4 of 5

What is the value of \( (4 + 2) \times 3 - 8 / 2 \)?

Question 5 of 5

Which expression correctly forces addition before multiplication in the expression \( a + b * c \)?