A function-like macro pastes its argument's text wherever the parameter appears in the body, so if the parameter appears more than once, the argument is evaluated more than once. #define MAX(a,b) ((a)>(b)?(a):(b)) references a twice, so MAX(i++, n) expands to ((i++) > (n) ? (i++) : (n)), i++ can execute twice, leaving i wrong and possibly returning the wrong value. The same hazard applies to function calls (MAX(read_sensor(), n) reads the sensor twice) and to volatile/register reads, where a second read can have a real hardware side effect. A real function evaluates each argument exactly once before the body runs, so the fix is almost always to use a static inline function instead of a function-like macro for anything whose arguments might have side effects.
C Programming · Interview question
What is double evaluation in a macro, and why does it matter?
A strong answer
What a weak answer sounds like
You know the answer. Do you know what gets you dinged?
Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.
From the lesson
The Preprocessor
Text substitution before the compiler ever runs: object and function macros, header guards, conditional compilation, and why a naive MAX macro evaluates its argument twice.