Arithmetic Operators
& Precedence
Master C++ arithmetic operators, understand operator precedence, and learn how expressions are evaluated step by step.
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.
The Five Binary Operators
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++.
The Modulus Operator %
The modulus operator returns the remainder after integer division. It works only with integers in C++.
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.
The Precedence Hierarchy
Left-to-Right Associativity
When two operators have equal precedence, C++ resolves ties using associativity. All binary arithmetic operators associate left to right.
Parentheses Override Everything
Parentheses have the highest precedence. Anything inside ( ) is evaluated first, regardless of other rules.
\( = 3 + 8 = \) 11
\( = 7 \times 2 = \) 14
Evaluating Expressions: Step by Step
Example 1: Simple Expression
Evaluate: \( 2 + 3 \times 4 - 1 \)
Example 2: Parentheses Change the Result
Evaluate: \( (2 + 3) \times (4 - 1) \)
Example 3: Division & Modulus
Evaluate with int types: \( 17 / 3 + 17 % 3 \times 2 \)
Example 4: Nested Parentheses
Evaluate: \( ((8 - 2) \times (3 + 1)) / 4 \)
Common Mistakes
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 \)?