---
title: "Precedence and associativity"
subject: "Computer Programming"
chapter: 3
key: "semester_1/computer_programming:precedence-associativity"
source: https://www.bctengineer.com/q/semester_1/computer_programming/precedence-associativity
site: BCT Engineering
---

# Precedence and associativity

Computer Programming · Chapter 3: Introduction to ‘C’ programming

- Asked in: 2074 Asoj, 4 marks; 2072 Kartik, 3 marks (asked 2 times)
- Video: [C_21 Operators Precedence and Associativity in C | C programming Tutorials](https://www.youtube.com/watch?v=hOLAoo2p7gM) — Jenny's Lectures CS IT
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/precedence-associativity
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-3

## Question

Explain with an example the role that precedence and associativity of an operator play in the execution of an expression.

## Short answer

Precedence dictates which operator evaluates first when types differ (e.g., * before +). Associativity resolves order for same‑precedence operators (left‑to‑right for +, -, *, /; right‑to‑left for =). Example: a+b*c multiplies first; a-b-c left‑to‑right; x=y=z assigns right‑to‑left.

## Answer

**Precedence** decides which operator is evaluated first when an expression has different operators. **Associativity** decides the order (left to right or right to left) when operators of the *same* precedence appear together.

- Precedence: `*`, `/`, `%` are higher than `+`, `-`.
- Associativity: `+ - * /` are left to right; assignment `=` and unary operators are right to left.
- Brackets `( )` override both.

Example, with `a = 10, b = 5, c = 2`:
```c
r1 = a + b * c;   /* * first: 10 + 10 = 20 */
r2 = (a + b) * c; /* brackets first: 15 * 2 = 30 */
r3 = a - b - c;   /* left to right: (10-5)-2 = 3 */
x = y = z = 7;    /* right to left: z=7, then y=z, then x=y */
```
Printing `r1`, `r2`, `r3`, `x`, `y` and `z` gives `20 30 3 7 7 7`.

So precedence gives the correct result of mixed expressions without brackets, and associativity removes ambiguity in chains of equal-level operators. Without them, the same expression could give different values.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Precedence decides
   A. the direction for equal operators
   B. which operator is evaluated first
   C. variable types
   D. output format
   Answer: B. Different operators. (Key point: Precedence)

2. Associativity decides
   A. which operator is highest
   B. the data type
   C. the order when operators of the same precedence appear
   D. the brackets
   Answer: C. Left-to-right or right-to-left. (Key point: Associativity)

3. With a=10,b=5,c=2, a + b*c =
   A. 30
   B. 25
   C. 17
   D. 20
   Answer: D. * first: 10+10. (Key point: Example)

4. x = y = z = 7 is evaluated
   A. right to left
   B. left to right
   C. randomly
   D. by precedence only
   Answer: A. Assignment is right-associative. (Key point: Right to left)

## Related in Computer Programming

- [Data types and operators in C](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Adata-types-operators-precedence) — Computer Programming, chapter 3

## Related in other subjects

- [Hydration of cement and role of compounds](https://www.bctengineer.com/question.md?key=semester_5%2Fconcrete_technology_and_masonry_structure%3Ahydration-compounds-strength) — Concrete Technology and Masonry Structure, chapter 1, asked 3×
- [Parallelism, IPC deadlock and OS role](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aparallelism-deadlock-os-role) — Microprocessor, chapter 6
- [NO₂ and SO₂ sources; role in acid rain](https://www.bctengineer.com/question.md?key=semester_2%2Fengineering_chemistry%3Ano2-so2-acid-rain) — Engineering Chemistry, chapter 3
- [ER diagram: cricket tournament](https://www.bctengineer.com/question.md?key=semester_6%2Fdatabase_management_system%3Aer-cricket-tournament) — Database Management System, chapter 2
