Page Content

Tutorials

What Is C Operator Precedence And Associativity?

C Operator Precedence and Associativity

The core ideas of operator precedence and associativity in C language and C++ dictate the sequence in which operators are evaluated in complicated expressions. Writing expressions that are successfully evaluated by the compiler requires an understanding of these rules.
The term “operator precedence” describes the order of importance given to certain operators. Higher precedence operators are evaluated before lower precedence operators when an expression comprises several operators with varying levels of precedence. As an illustration, the operators for multiplication (*), division (/), and modulus (%) usually have precedence over the addition (+) and subtraction (-) operators.

When two or more operators with the same level of precedence are present in an expression, the associativity rule is applied. It chooses whether to analyse the operators from right to left or left to right. For instance, the multiplicative operators (*, /, %) and the additive operators (+ and -) usually have left-to-right associativity, whereas the conditional operator (?:), unary operators, and assignment operators (=, +=, etc.) usually have right-to-left associativity.

By using brackets (), the default precedence and associativity rules can be overridden. First, expressions included in parenthesis are assessed. The innermost brackets are assessed first in a nested pair of brackets. Code that uses brackets is easier to comprehend and avoids unexpected evaluation orders.

Let’s look at an example as show below how to associativity and precedence function: 9–12 / 3 + 3 * 2–1.

The following criteria should be followed when assessing mathematical statements without brackets: Multiplication (*) and Division (/) are assessed first, followed by Modulo (%), and then Addition (+) and Subtraction (-). Associativity rules apply within the same precedence level (usually left-to-right for these operators).

Let’s take a step-by-step look at 9 – 12 / 3 + 3 * 2 – 1:

Step 1: Determine which operators are -, /, +, *, and -.

Step 2: Apply precedence: Priority is higher for division and multiplication than for addition and subtraction. The additions and subtraction will be carried out when the procedures 12 / 3 and 3 * 2 have been completed.

Step3: Apply associativity from left to right for / and *:

  • As the operator on the left of / and *, 12 / 3 is evaluated first. A 12/ 3 yields a 4. Hence, the formula is 9 – 4 + 3 * 2 – 1.
  • Then, 3 * 2 is assessed. 6 is the result of 3 * 2. Nine minus four plus six plus one is the result.

Step 4: Only the operators for addition (+) and subtraction (-), which have the same precedence, remain in the phrase. The associativity of these operators is from left to right.

Step 5: Apply the left-to-right associativity of + and -:

  • We start by evaluating 9–4. 5 is the result of 9 – 4. 5 + 6 – 1 is the new expression.
  • Next is the evaluation of 5 + 6. Eleven is the result of 5 plus 6. The formula changes to 11 – 1.
  • Eleven to one is finally assessed. 10 is the result of 11-1.

When 9 – 12 / 3 + 3 * 2 – 1 is calculated, the result is 10.

Now look at how brackets change assessment order. Consider the following phrase: 9 – (12/3 + 3*2) – 1

Step 1: Evaluation begins with (12/3 + 3*2) in parenthesis.

Step 2: 12/3 and 3*2 in parenthesis are evaluated using priority (/ and * before +). * and / are left-to-right associative.

  • Parenthesis phrase becomes 4 + 3 * 2 after evaluating 12 / 3.
  • The answer to 3 * 2 is 6. Parenthesis expression becomes 4 + 6.
  • 10 is the evaluation of 4 + 6. Ten is the value of the expression enclosed in parenthesis.

Step 3: The initial expression is changed to 9 – 10 – 1.

Step 4: Subtraction operators are the only ones left, and they share the same precedence and left-to-right associativity.

Step 5: Assess from left to right:

  • 9 – 10 is evaluated: -1. The expression becomes -1 – 1.
  • -1 – 1 is assessed as -2.
  • When 9 – (12/3 + 3*2) – 1 is calculated, the result is -2.

Parentheses, as shown, alter the sequence of events and produce a new outcome. Parentheses are advised in order to prevent errors and clearly indicate the desired sequence of evaluation.

Agarapu Geetha
Agarapu Geetha
My name is Agarapu Geetha, a B.Com graduate with a strong passion for technology and innovation. I work as a content writer at Govindhtech, where I dedicate myself to exploring and publishing the latest updates in the world of tech.