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
Identify types: Check whether operands are integers or floating-point values.
Check parentheses: Use parentheses to identify explicitly grouped parts.
Apply precedence and associativity: Determine which operands belong to each operator.
Calculate valid operations: Use integer or floating-point arithmetic as required.
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
Expression
Reasoning
Result
8 + 12 / 5 * 2
12 / 5 = 2; 2 * 2 = 4; 8 + 4
12 (int)
(8 + 12) / 5 * 2
20 / 5 = 4; 4 * 2
8 (int)
5 / 2 + 0.5
Integer division gives 2, then convert and add 0.5