What is the double-evaluation trap in macros?
0 upvotes
Practice with AISoon
Function-like macros expand their arguments textually, so any argument with side effects is evaluated multiple times:
c
#define MAX(a, b) ((a) > (b) ? (a) : (b))int x = MAX(i++, j++); // i or j incremented TWICE
The winning argument is evaluated once in the comparison and once in the result — causing the increment to happen twice. This is why static inline functions are preferred when possible — they evaluate each argument exactly once while still being inlined by the compiler. The macro version is still necessary for type-generic operations in C (before C11's _Generic).
Source: C / C++ Concepts Q&A
