Define an expression and explain how its type affects the result.
5 marks
Answer in brief
An expression combines values, variables, operators or function calls to perform a calculation or another operation.
Parts of an expression
Operands: The values or objects used, such as a, b and 10.
Operators: The operations performed, such as + or *.
Parentheses: Show the intended grouping, such as (a + b) * 2.
Types and examples
Arithmetic: a + b * 2 calculates a numeric result.
Relational: a > b produces 1 or 0.
Logical: a > 0 && b > 0 tests two conditions.
Assignment: total = 5 stores a value.
Conditional: a > b ? a : b selects a value.
Expression and expression statement
a + b is an expression. Adding a semicolon gives the expression statement a + b;. That statement calculates the sum and discards it. total = a + b; is useful because it stores the result. int total = 5; is a declaration with initialisation, rather than an expression statement.
Evaluation example
For a = 4 and b = 3, a + b * 2 gives 10 because multiplication groups before addition. (a + b) * 2 gives 14. Operand types also matter: 7 / 2 gives 3, whereas 7.0 / 2 gives 3.5. Avoid expressions that change the same variable without the ordering required by C.
Reference table
↔ Scroll horizontally on a small screen
Expression examples
Expression
What it does
a + b * 2
Computes an arithmetic value.
score >= 40
Produces an int truth value, 0 or 1.
total = 5
Stores a value and has the assigned value as its result.
count++
Modifies count and supplies its previous value.
printf("Hello")
Calls a function; the call also has a return value.
Key points
Grouping, types and sequencing all matter.
An expression can have a value and also change program state.