Macro extension for macros with arguments and variables of the same name

Consider the following C program (ignore the double side effect problem):

#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;
}

This is pretty nice since you don’t have to worry about inadvertent collisions between maxand max(). The GCC manual says:

A functional macro expands only if its name appears with a pair of parentheses after it. If you write only a name, it stays alone

Is it standardized or is it just something done by convention?

+5
source share
1 answer

Yes, the behavior here is clearly defined.

max - (.. , ).

max - , max . , max:

int max;
max = 42;

:

max(1, 2)
max (1, 2)
max
(
    1, 2
)
max()

( , , . , .)

C langauge. C99 Β§6.10.3/10 , , ,

- , ( , , ( ).

+5

All Articles