Project 4: Expression and Operator Mastery
A test suite demonstrating operator precedence, associativity, sequence points, and evaluation order - including cases that break.
Quick Reference
| Attribute | Value |
|---|---|
| Primary Language | C |
| Alternative Languages | None |
| Difficulty | Level 3 - Advanced |
| Time Estimate | See main guide |
| Knowledge Area | Language Semantics, Compilation |
| Tooling | GCC, Clang, Godbolt |
| Prerequisites | See main guide |
What You Will Build
A test suite demonstrating operator precedence, associativity, sequence points, and evaluation order - including cases that break.
Why It Matters
This project builds core skills that appear repeatedly in real-world systems and tooling.
Core Challenges
- Precedence traps → Maps to correct expression writing
- Sequence point violations → Maps to avoiding undefined behavior
- Short-circuit evaluation → Maps to efficient conditional logic
Key Concepts
- Map the project to core concepts before you code.
Real-World Outcome
# 1. Precedence surprises
$ ./expr_test precedence
Expression: a & 0x0F == b
Parsed as: a & (0x0F == b) // Comparison has higher precedence!
You probably meant: (a & 0x0F) == b
Expression: ptr->field++
Parsed as: (ptr->field)++ // Correct - -> binds tighter than ++
# 2. Sequence point violations
$ ./expr_test sequence
UNDEFINED: i = i++ + ++i;
Compiler 1 (-O0): 7
Compiler 1 (-O3): 5
Compiler 2 (-O0): 6
WARNING: Different results! This is undefined behavior.
DEFINED: i = i + 1; j = i + 1;
Consistent result: i=6, j=7 (sequence point at semicolon)
# 3. Short-circuit evaluation
$ ./expr_test shortcircuit
Expression: func1() && func2()
func1() returns 0 (false)
func2() NOT CALLED (short-circuit)
Result: 0
Expression: func1() & func2() // Bitwise AND
func1() returns 0
func2() CALLED (no short-circuit for bitwise ops!)
Result: 0
# 4. Pointer arithmetic
$ ./expr_test pointer_arith
int arr[5] at 0x7fff5000
arr + 1 = 0x7fff5004 (moved by sizeof(int) = 4 bytes)
&arr + 1 = 0x7fff5014 (moved by sizeof(arr) = 20 bytes!)
Implementation Guide
- Reproduce the simplest happy-path scenario.
- Build the smallest working version of the core feature.
- Add input validation and error handling.
- Add instrumentation/logging to confirm behavior.
- Refactor into clean modules with tests.
Milestones
- Milestone 1: Minimal working program that runs end-to-end.
- Milestone 2: Correct outputs for typical inputs.
- Milestone 3: Robust handling of edge cases.
- Milestone 4: Clean structure and documented usage.
Validation Checklist
- Output matches the real-world outcome example
- Handles invalid inputs safely
- Provides clear errors and exit codes
- Repeatable results across runs
References
- Main guide:
PROFESSIONAL_C_PROGRAMMING_MASTERY.md - Effective C, 2nd Edition by Robert C. Seacord