ramyasri Amudala
PRO
last month
Ramyasricountry asked

What is this precedence and associativity?

Abhay Jajodia
Expert
last month
Abhay Jajodia answered

Great question — let’s break it down clearly.

Operator precedence decides which operation is performed first in an expression. For example, multiplication and division have higher priority than addition and subtraction. So in this expression:

9 / 3 + 8 * 4 - 2

C++ first evaluates 9 / 3 and 8 * 4, then performs the addition and subtraction.

Associativity decides the direction in which operators are evaluated when they have the same priority. In C++, most arithmetic operators are evaluated from left to right. So in:

3 + 32 - 2

the addition is done before the subtraction.

To make expressions easier to read and control, you can use parentheses, like this:

(9 / 3) + (8 * 4) - 2

This clearly shows which parts are calculated first.

C++
This question was asked as part of the Learn C++ Basics course.