C++ Operator Precedence

Operator precedence tells C++ how to group an expression that contains different operators. Operators with higher precedence bind more tightly. In 2 + 3 * 4, multiplication has higher precedence than addition. C++ groups it as 2 + (3 * 4), so the result is 14, not 20.

How Operator Precedence Works

An operator is a symbol that performs an action. The + operator adds values, and the * operator multiplies them. An expression is code that produces a value.

Each C++ operator has a precedence level. A higher level makes an operator bind more tightly to its operands. An operand is a value on which an operator works.

Example Without Parentheses

Consider 2 + 3 * 4. Multiplication has higher precedence than addition, so C++ groups the multiplication:

2 + (3 * 4)

First, 3 * 4 produces 12. The remaining expression is 2 + 12, which produces 14.

Use Parentheses to Control Grouping

Parentheses make your intended grouping clear. In (2 + 3) * 4, the addition is inside parentheses:

(2 + 3) * 4 becomes 5 * 4, which produces 20.

Common precedence levels used in this lesson
Priority Operators Purpose
Controlled grouping ( ) Groups the enclosed expression
Higher * / % Multiplication, division, and remainder
Lower + - Addition and subtraction
Precedence decides grouping. It does not, by itself, decide which operand C++ evaluates first.

Operator Precedence Visual Guide

The two panels use the same numbers. Parentheses change the grouping and the final result.

C++ operator precedence with and without parentheses The top panel shows multiplication binding before addition to produce 14. The bottom panel shows parentheses grouping addition first to produce 20. Which Part Binds First? Normal precedence compared with parentheses NORMAL PRECEDENCE 2 + 3 * 4 2 + (3 * 4) 2 + 12 = 14 PARENTHESES (2 + 3) * 4 5 * 4 5 * 4 = 20 Clear parentheses help readers understand your code.

Example

This program shows normal operator precedence and two expressions that use parentheses.

#include <iostream>

int main() {
    int normal_result = 2 + 3 * 4;
    int grouped_result = (2 + 3) * 4;
    int division_result = 18 / 3 + 2;

    std::cout << "Normal precedence: " << normal_result << '\n';
    std::cout << "With parentheses: " << grouped_result << '\n';
    std::cout << "Division before addition: " << division_result << '\n';

    return 0;
}

Output

Normal precedence: 14
With parentheses: 20
Division before addition: 8

The first expression groups multiplication. The second groups addition because of its parentheses. In the third expression, division has higher precedence than addition, so 18 / 3 produces 6 before 2 is added.

Common Mistakes

Always reading from left to right

Wrong idea: 2 + 3 * 4 becomes 5 * 4.

Correction: Multiplication has higher precedence, so the correct grouping is 2 + (3 * 4).

Confusing precedence with associativity

Wrong idea: Precedence decides what happens when operators have the same level.

Correction: Precedence compares different levels. Associativity handles operators at the same level.

Making the reader guess

Unclear: A long expression may be correct but hard to read.

Correction: Add useful parentheses. They show your intended grouping and reduce mistakes.

Short Practice

  1. Predict the result of 5 + 2 * 3.
  2. Add parentheses so 5 + 2 * 3 produces 21.
  3. Predict the result of 20 - 12 / 3.

Frequently Asked Questions

Does operator precedence mean evaluation order?

No. Precedence decides how an expression is grouped. It does not, by itself, say which operand C++ evaluates first.

Can parentheses change the result?

Yes. Parentheses can force a different grouping, so the expression may produce a different result.

What happens when operators have the same precedence?

C++ uses the operators' associativity rule to decide their grouping.

Summary

C++ operator precedence decides how different operators are grouped. Multiplication, division, and remainder have higher precedence than addition and subtraction. Parentheses can force a different grouping and make code easier to understand. Precedence controls grouping, not operand evaluation order.