Skip to study material
← Question index

QUESTION 69 · Expressions and conversions

Define precedence and associativity with examples.

2 marks

Answer

Precedence determines grouping across operator levels; associativity resolves grouping within one level.

  • 2 + 3 * 4 groups as 2 + (3 * 4), giving 14.
  • 20 / 5 * 2 groups as (20 / 5) * 2, giving 8. Grouping does not generally specify operand evaluation order.

QUESTION 70 · Expressions and conversions

Explain operator precedence and associativity with the correct precedence table.

5 marks

Answer in brief

Precedence and associativity determine how operators and operands are grouped in an expression.

Precedence

Precedence gives the priority between different operator levels. Multiplication has higher precedence than addition. Therefore 2 + 3 * 4 groups as 2 + (3 * 4) and gives 14. Parentheses can change the grouping: (2 + 3) * 4 gives 20.

Associativity

Associativity decides grouping when operators have the same precedence. Multiplication and division group from left to right. Thus 20 / 5 * 2 means (20 / 5) * 2 and gives 8. Assignment groups from right to left, so a = b = 5 means a = (b = 5).

Common priority levels

  1. Unary operations: Operators such as ! and unary minus group before multiplication.
  2. Multiplication level: *, / and % group before addition and subtraction.
  3. Comparison levels: Relational operators group before equality operators.
  4. Logical levels: && groups before ||.
  5. Assignment: Assignment has lower precedence than these arithmetic and logical operators. Refer to the complete table for all levels.

Important distinction

Grouping is not generally the order in which operands run. For example, multiplication grouping does not say which of two function calls runs first in f() * g(). Write separate statements when order matters. Parentheses improve readability but do not make an otherwise invalid multiple update valid.

Reference table

↔ Scroll horizontally on a small screen
C operator grouping, highest level first
LevelOperators or formsAssociativity
1Postfix: () call, [] subscript, . and ->, postfix ++ --Left to right
2Prefix ++ --, unary + -, ! ~, * indirection, & address, sizeof, _Alignof, casts (type)Right to left
3* / %Left to right
4+ -Left to right
5<< >>Left to right
6< <= > >=Left to right
7== !=Left to right
8Bitwise &Left to right
9Bitwise ^Left to right
10Bitwise |Left to right
11Logical &&Left to right
12Logical ||Left to right
13?:Right to left
14= += -= *= /= %= <<= >>= &= ^= |=Right to left
15Comma operator ,Left to right

Key points

  • Multiplication and addition must not share a precedence row.
  • Relational, equality, bitwise and logical levels are distinct.

Related exam questions

Topic matches from the supplied JNTUH papers. Marks below are the printed allocations for those questions.

  • September 2023 · Q1(b) · 3 marks

    What are operator precedence rules?

    Paper P09 · page 1
  • January/February 2024 · Q3(b) · 6 marks

    Discuss the role of operator precedence in expression evaluation, with examples.

    Paper P10 · page 1
View all matching exam references →