Per C 2018 6.10.3.1, a compiler first identifies the arguments for a function-like macro and then performs macro replacement on the arguments, followed by macro replacement for the function-like macro. This means that, in ADD(A_AND_B)
, the argument is identified as A_AND_B
before it is replaced with 5,1
. As the macro invocation has only this single argument and the macro is defined to have two parameters, an error is diagnosed.
Given your definition of ADD
, there is no way to change this behavior in a compiler that conforms to the C standard.
You can instead use another macro to expand the arguments and apply the desired macro:
#define Apply(Macro, Arguments) Macro(Arguments)
then int result = Apply(ADD, A_AND_B);
will work. That will identify ADD
and A_AND_B
as arguments to Apply
. Then it will expand those, producing an unchanged ADD
and 5,1
. Then the macro replacement for Apply
produces ADD(5,1)
. Then this is again processed for macro replacement, which replaces ADD(5,1)
in the ordinary way.
Note that good practice is usually to define ADD
as #define ADD(a, b) ((a) + (b))
to avoid unexpected interactions with other operators neighboring the use of the macro.