Skip to main content

Checkpoint: Macros Under Judgment

Challenge on lesson: Checkpoint: Macros Under Judgment

CHALLENGE
Difficulty: advanced+25 XP

Consolidate the preprocessor's compile-time powers โ€” and exercise its most famous trap. The starter defines TAG, the counting helper side(), and the function ca9hello(). Implement: 1. int max_of(int a, int b) โ€” deliberately a FUNCTION, not a macro: a macro that evaluates each argument exactly once is impossible in ISO C, and safe_max_probe() must return 1 after computing max_of(side(), 4) because side() ran exactly once. 2. #define IS_POWER2(n) ... โ€” true iff n is a positive power of two (the classic (n & (n-1)) == 0 with n > 0). Safe as a macro here because it is only used on literals with no side effects. 3. #define CAT(a, b) CAT2(a, b) with #define CAT2(a, b) a##b โ€” the two-level indirection so arguments are expanded before pasting: CAT(ca9, TAG) must become the call target ca9hello. 4. int eval_count(void) returning the running total of side() calls โ€” the observable proof of evaluation discipline.

Back to lesson: Checkpoint: Macros Under Judgment