Skip to study material
← Question index

QUESTION 72 · Expressions and conversions

Evaluate C expressions step by step and distinguish grouping from evaluation order.

5 marks

Answer in brief

Expression evaluation means finding an expression’s result by applying its grouping rules, operand types and operator meanings.

Steps

  1. Identify types: Check whether operands are integers or floating-point values.
  2. Check parentheses: Use parentheses to identify explicitly grouped parts.
  3. Apply precedence and associativity: Determine which operands belong to each operator.
  4. Calculate valid operations: Use integer or floating-point arithmetic as required.
  5. Check the result: Make sure division is valid and values stay within the supported range.

Worked example: integer arithmetic

Evaluate 8 + 12 / 5 * 2. Division and multiplication have higher precedence than addition and group left to right. First, 12 / 5 gives 2 because both operands are integers. Next, 2 * 2 gives 4. Finally, 8 + 4 gives 12. Thus the result is 12.

Worked example: floating-point arithmetic

In 8 + 12.0 / 5 * 2, 12.0 is double. The division gives approximately 2.4, multiplication gives approximately 4.8, and addition gives approximately 12.8. Changing an operand type can therefore change the answer.

Important points

Do not assume that all operands are evaluated from left to right. Short-circuit operators provide particular ordering rules, but many operators do not. An expression such as i++ + ++i has undefined behaviour and no guaranteed numeric result. Divide only by a valid non-zero divisor, and avoid signed overflow.

Reference tables

↔ Scroll horizontally on a small screen
Evaluation without ambiguous side effects
ExpressionReasoningResult
8 + 12 / 5 * 212 / 5 = 2; 2 * 2 = 4; 8 + 412 (int)
(8 + 12) / 5 * 220 / 5 = 4; 4 * 28 (int)
5 / 2 + 0.5Integer division gives 2, then convert and add 0.52.5 (double)
5 / 2.0 + 0.5Floating division gives 2.5, then add 0.53.0 (double)
3 < 4 && 9 != 0Both comparisons are true1 (int)
↔ Scroll horizontally on a small screen
Trace 8 + 12 / 5 * 2
SubexpressionTypeResult
12 / 5int divided by int2
(12 / 5) * 2int multiplication4
8 + ((12 / 5) * 2)int addition12

Diagram or flowchart

The root addition combines 8 with a multiplication; that multiplication combines the integer quotient 12 divided by 5 with 2, producing 12 overall.
Expression grouping tree · Download diagram

Key points

  • Integer division happens before a later conversion can preserve a fraction.
  • Undefined behaviour is not a puzzle with one portable numerical answer.