Skip to main content

Practice ยท 1 of 1

Write the Safe Macros

The boilerplate defines the macros under test. Your job is to implement the FUNCTIONS that use them correctly โ€” and one macro: ``c /* provided (correct): */ #define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0])) #define MAXI(a, b) ((a) > (b) ? (a) : (b)) /* implement the function: sum of array using ARRAY_LEN (no explicit n) */ long sum_all(const int *a, size_t n); /* implement: clamp v into [lo, hi] using MAXI twice */ int clamp(int v, int lo, int hi); /* implement THE MACRO (the boilerplate declares it for you to fill): runs stmt exactly once per call, usable in if/else without braces */ #define RUN_ONCE(stmt) ... ` Careful: ARRAY_LEN only works on real arrays โ€” sum_all takes a pointer, which is why it needs n`.

Difficulty: intermediate

Back to lesson: Practice: Macro Gym