Consider the following C program (ignore the double side-effect issue):
#define max(a, b) (a>b?a:b)
int main(void){
int max = max(5,6);
return max;
}
The GCC preprocessor turns this into:
int main(void){
int max = (5>6?5:6);
return max;
}
Which is quite nice, since you don’t have to worry about unintentional collisions between max and max(). The GCC manual says:
A function-like macro is only expanded if its name appears with a pair of parentheses after it. If you write just the name, it is left alone
Is this standardized or just something done by convention?
Yes, the behavior here is well-defined.
Your macro
maxis a function-like macro (i.e., when you define it, its name is followed immediately by a left parenthesis and it takes arguments).A use of
maxlater in your code is only an invocation of that macro if the use ofmaxis followed by a left parenthesis. So, these would not invoke themaxmacro:But these would all invoke the max macro:
(Note that the last line is ill-formed because the number of arguments does not match the number of parameters. This is still a macro invocation, though, and would cause a compilation error.)
This behavior is mandated by the C langauge standard. C99 §6.10.3/10 states that after a function-like macro has been defined,