What is a##b & #a?
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)
main()
{
printf("%s\n",h(f(1,2))); //how should I interpret this?? [line 1]
printf("%s\n",g(f(1,2))); //and this? [line 2]
}
How does this program work?
The output is
12
f(1, 2)
now I understand how a##b & #a work. But why is the result different in the two cases (line 1 and line 2)?
The ## concatenates two tokens together. It can only be used in the preprocessor.
f(1,2)becomes1 ## 2becomes12.The # operator by itself stringifies tokens:
#abecomes"a". Therefore,g(f(1,2))becomes"f(1,2)"when the preprocessor is done with it.h(f(1,2))is effectively#(1 ## 2)which becomes#12which becomes"12"as the preprocessor runs over it.