How does operator precedence work in C++?

Can you clarify the rules around operator precedence in C++? How can I control the order of operations?

Operator precedence in C++ determines the order in which operators are evaluated in an expression when there are multiple operators. If operators have the same precedence, they are evaluated based on their associativity (left-to-right or right-to-left).

Here's a quick summary of the precedence and associativity of some common operators, from highest to lowest precedence:

  1. Postfix operators (e.g., ++, -) - left to right
  2. Prefix operators (e.g., ++, -, !) - right to left
  3. Multiplicative operators (, /, %) - left to right
  4. Additive operators (+, ) - left to right
  5. Relational operators (<, <=, >, >=) - left to right
  6. Equality operators (==, !=) - left to right
  7. Logical AND (&&) - left to right
  8. Logical OR (||) - left to right
  9. Assignment operators (=, +=, =, etc.) - right to left

For example:

// a is 17, because * has
// higher precedence than +
int a = 5 + 3 * 4;

// b is true, because && has
// higher precedence than ||
bool b = true || false && false;

If you want to override the default precedence, you can use parentheses. Expressions inside parentheses are always evaluated first. For example:

// a is 32, because (5 + 3) is evaluated first
int a = (5 + 3) * 4;

// b is false, because (true || false)
// is evaluated first
bool b = (true || false) && false;

It's often a good idea to use parentheses even when they're not strictly necessary, as they can make your code more readable and less prone to errors.

Also, keep in mind that the precedence and associativity rules can interact in complex ways when there are many operators in a single expression. When in doubt, use parentheses to make your intent clear.

Variables, Types and Operators

Learn the fundamentals of C++ programming: declaring variables, using built-in data types, and performing operations with operators

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

How does integer division work in C++?
Can you explain more about how integer division and the modulus operator work in C++, with some examples?
Implicit casts between booleans and numbers in C++
The lesson mentions that numeric types can be implicitly cast to booleans in C++, with 0 being false and anything else true. Can you clarify how that works with an example?
What is uniform initialization in C++?
The lesson recommends using uniform initialization with braces (like int x {5};) instead of the = operator. What advantages does this have?
When should I use auto in C++?
The auto keyword seems convenient, but the lesson says to use it sparingly. When is it appropriate to use auto in C++?
Prefix vs postfix increment in C++
What's the difference between the prefix and postfix increment/decrement operators in C++? When should I use one over the other?
Floating point precision in C++
I've heard that floating point numbers can sometimes be imprecise in C++. Can you explain why this happens and how to handle it?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant