Learn with Yasir

Share Your Feedback

C++ Operator Precedence


Learn C++ operator precedence with clear examples and simple explanations. Understand how arithmetic, relational, logical, assignment, unary, and increment/decrement operators are evaluated in C++ programs.

1. What is Operator Precedence?

Operator precedence tells C++ which operator should be performed first when an expression contains more than one operator.

Example

int result = 5 + 3 * 2;

Multiplication * has higher precedence than addition +.

So:

5 + (3 * 2)
= 5 + 6
= 11

Answer: 11


2. Basic Rule

Remember:

Higher precedence → performed first

For example:

10 - 4 / 2

/ has higher precedence than -.

10 - (4 / 2)
= 10 - 2
= 8

3. Common Operator Precedence in C++

For beginners, remember this order:

Priority Operators Meaning    
1 () Parentheses    
2 ++ -- Increment / Decrement    
3 * / % Multiplication, Division, Modulus    
4 + - Addition, Subtraction    
5 < <= > >= Relational    
6 == != Equality    
7 && Logical AND    
8 `   ` Logical OR
9 = += -= *= /= Assignment    

Easy way to remember

()
↓
++ --
↓
* / %
↓
+ -
↓
< <= > >=
↓
== !=
↓
&&
↓
||
↓
=

4. Parentheses Have the Highest Priority

Use parentheses () when you want something to be calculated first.

int result = (5 + 3) * 2;

First:

5 + 3 = 8

Then:

8 * 2 = 16

Answer: 16

Compare:

int result = 5 + 3 * 2;

Answer:

11

But:

int result = (5 + 3) * 2;

Answer:

16

5. *, /, and %

These operators have the same precedence.

int x = 20 / 5 * 2;

When operators have the same precedence, C++ generally evaluates them from left to right.

20 / 5 = 4
4 * 2 = 8

Answer: 8

Another example

int x = 17 % 5 + 2;

First %:

17 % 5 = 2

Then:

2 + 2 = 4

Answer: 4


6. + and -

+ and - have the same precedence and are evaluated from left to right.

int x = 10 - 3 + 2;

First:

10 - 3 = 7

Then:

7 + 2 = 9

Answer: 9



7. Example: Complete Expression

Consider:

int result = 5 + 2 * 3;

Step 1: *

2 * 3 = 6

Step 2: +

5 + 6 = 11

Therefore:

result = 11

8. Example with Parentheses

int result = (5 + 2) * 3;

Step 1

5 + 2 = 7

Step 2

7 * 3 = 21

Therefore:

result = 21

9. Important Rule: Left-to-Right

When two operators have the same precedence, they are generally evaluated from left to right.

Example:

int result = 20 / 5 * 2;
20 / 5 = 4
4 * 2 = 8

So:

Answer = 8

Not:

20 / (5 * 2) = 2

10. Best Practic

When an expression becomes difficult to understand, use parentheses.

Instead of:

int result = a + b * c - d / e;

Think of it as:

int result = a + (b * c) - (d / e);

Parentheses make the order clear and reduce mistakes.


Quick Revision

Remember this order:

Parentheses → Increment/Decrement → Multiply/Divide/Modulus → Add/Subtract → Relational → Equality → AND → OR → Assignment

()
++ --
* / %
+ -
< <= > >=
== !=
&&
||
=

Golden Rule ⭐

First check parentheses, then higher-precedence operators, and when operators have equal precedence, follow their associativity (commonly left-to-right for arithmetic operators).