#include <stdio.h>
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)
int main()
{
printf("%s\n",h(f(1,2)));
printf("%s\n",g(f(1,2)));
return 0;
}
Just by looking at the program one "might" expect the output to be, the same for both the printf statements. But on running the program you get it as:
bash$ ./a.out
12
f(1,2)
bash$
Why is it so?
An occurrence of a parameter in a function-like macro, unless it is the operand of #
or ##
, is expanded before substituting it and rescanning the whole for further expansion. Because g
's parameter is the operand of #
, the argument is not expanded but instead immediately stringified ("f(1,2)"
). Because h
's parameter is not the operand of #
nor ##
, the argument is first expanded (12
), then substituted (g(12)
), then rescanning and further expansion occurs ("12"
).