Operator precedence - Time & Space Complexity
When we look at operator precedence in C, we want to see how the order of operations affects how many steps the program takes.
We ask: Does the order change how long the program runs as input grows?
Analyze the time complexity of the following code snippet.
int compute(int a, int b, int c) {
int result = a + b * c / 2 - 5;
return result;
}
This code calculates a value using addition, multiplication, division, and subtraction in one line.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Simple arithmetic operations executed once.
- How many times: Each operation runs exactly one time per function call.
Since the operations happen once, the time does not grow with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 4 |
| 100 | 4 |
| 1000 | 4 |
Pattern observation: The number of steps stays the same no matter how big the inputs are.
Time Complexity: O(1)
This means the program takes the same amount of time no matter the input size.
[X] Wrong: "Changing the order of operators will make the program slower or faster."
[OK] Correct: Operator order affects calculation results but does not change how many steps the program takes.
Understanding operator precedence helps you read code clearly and reason about its speed, a useful skill in many programming tasks.
"What if the expression included a loop repeating the calculation n times? How would the time complexity change?"
Practice
3 + 4 * 5?Solution
Step 1: Recall operator precedence rules
Multiplication and division have higher precedence than addition and subtraction in C.Step 2: Identify operators in the expression
The expression has addition (+) and multiplication (*). Multiplication runs first.Final Answer:
Multiplication (*) -> Option DQuick Check:
Multiplication > Addition [OK]
- Thinking addition runs before multiplication
- Ignoring operator precedence
- Assuming left to right always applies
Solution
Step 1: Understand the desired calculation
Add 2 and 3 first, then multiply by 4.Step 2: Check parentheses usage
Only (2 + 3) * 4 uses parentheses to add 2 and 3 first, then multiply by 4.Final Answer:
(2 + 3) * 4 -> Option AQuick Check:
Parentheses change order to add first [OK]
- Ignoring parentheses and trusting default precedence
- Misreading the expression order
- Assuming left to right overrides precedence
int x = 5 + 3 * 2 - 4 / 2;
printf("%d", x);Solution
Step 1: Apply operator precedence
Multiplication and division first: 3 * 2 = 6, 4 / 2 = 2.Step 2: Calculate the expression left to right
Expression becomes 5 + 6 - 2 = 9.Final Answer:
9 -> Option BQuick Check:
5 + 6 - 2 = 9 [OK]
- Adding before multiplying
- Dividing after subtraction
- Calculating left to right ignoring precedence
int result = 10 / 2 * 3 + 4 - 5;
Solution
Step 1: Check operator precedence and associativity
Multiplication (*) and division (/) have the same precedence and are evaluated left to right.Step 2: Verify the expression evaluation order
Expression evaluates as ((10 / 2) * 3) + 4 - 5, which is correct.Final Answer:
Multiplication and division have the same precedence and are left to right -> Option AQuick Check:
* and / same precedence, left to right [OK]
- Thinking division happens after addition
- Assuming addition has higher precedence
- Ignoring left-to-right rule for same precedence
int y = 8 - 3 + 2 * 4 / 2;, which parentheses placement changes the result to 14?Solution
Step 1: Calculate original expression
* and / first left-to-right: 2 * 4 = 8, 8 / 2 = 4; then 8 - 3 + 4 = 9.Step 2: Evaluate (8 - 3 + 2) * 4 / 2
(8 - 3 + 2) = 7, 7 * 4 = 28, 28 / 2 = 14.Step 3: Confirm others
8 - (3 + 2) * 4 / 2 = 8 - 20 / 2 = 8 - 10 = -2; 8 - 3 + (2 * 4) / 2 = 9; (8 - (3 + 2)) * 4 / 2 = 3 * 4 / 2 = 6.Final Answer:
(8 - 3 + 2) * 4 / 2 -> Option CQuick Check:
Parentheses force addition before multiplication [OK]
- Assuming original order always applies
- Not recalculating expression after adding parentheses
- Misplacing parentheses without recalculating
Ready to prove it?
Pass the test and this pattern moves unknown
