← Back to C++ Course | Chapter 3: Operators | Lesson 9 of 9

C++ Operator Precedence

Operator precedence is the order C++ follows when a line has several operators, like doing multiplication before addition in school. Brackets let you decide the order yourself.
Syntax
cpp
result = a + b * c;    // * / % run before + -
result = (a + b) * c;  // parentheses run first

Arithmetic Operator Precedence

C++ applies the same order-of-operations rules taught in basic math: multiplication and division are evaluated before addition and subtraction, so 2 + 3 * 4 evaluates to 14, not 20.

Misjudging precedence is a common source of subtly wrong calculations that compile without any error.

Example: Arithmetic Operator Precedence

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare result and set it to 2 + 3 * 4
	int result = 2 + 3 * 4;
	// Print to the console with cout
	std::cout << result << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Using Parentheses to Force Precedence

Wrapping part of an expression in parentheses forces it to be evaluated first, overriding the default precedence rules entirely — (2 + 3) * 4 evaluates to 20 precisely because the addition is now grouped explicitly.

When in doubt about how an expression will be evaluated, adding parentheses costs nothing and removes all ambiguity for both the compiler and future readers.

Example: Using Parentheses to Force Precedence

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare result and set it to (2 + 3) * 4
	int result = (2 + 3) * 4;
	// Print to the console with cout
	std::cout << result << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Relational and Logical Precedence

Relational operators like > and < bind more tightly than logical operators like && and ||, which is why a > 5 && b < 10 is parsed as (a > 5) && (b < 10) without needing explicit parentheses around each comparison.

Relying on this default ordering without parentheses is fine for simple conditions, but larger expressions benefit from explicit grouping for clarity.

Example: Relational and Logical Precedence

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int a = 6, b = 5;
	// Declare result and set it to a > 5 && b < 10
	bool result = a > 5 && b < 10;
	// Print to the console with cout
	std::cout << result << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Left-to-Right Associativity

When two operators share the same precedence level, such as a - b + c, C++ evaluates them left to right, treating it as (a - b) + c.

This left-to-right rule is what most arithmetic and comparison operators follow, and it matches the way you'd naturally read the expression.

Example: Left-to-Right Associativity

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int a = 10, b = 3, c = 2;
	// Declare result and set it to a - b + c
	int result = a - b + c;
	// Print to the console with cout
	std::cout << result << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Right-to-Left Associativity of Assignment

Assignment is an exception to left-to-right evaluation — it associates right to left, which is exactly what makes chained assignment like a = b = c = 0; work: c = 0 happens first, then that result is assigned to b, and finally to a.

This lets you initialize several variables to the same value in one compact line.

Example: Right-to-Left Associativity of Assignment

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int a, b, c;
	a = b = c = 5;
	// Print to the console with cout
	std::cout << a << " " << b << " " << c << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Assuming a + b * c evaluates left to right, when multiplication happens before addition.
  2. Writing a & b == c, where == binds tighter than & and gives a surprising result.
  3. Not using parentheses to make intent clear, so readers and the compiler group operations differently than expected.
Chapter Summary
  • Arithmetic, relational, logical, bitwise, and assignment operators perform calculations and comparisons.
  • Increment and decrement operators change a value by one, and the ternary operator is a short conditional.
  • Operator precedence determines the order in which expressions are evaluated.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.