I have a test program like below.
#define TEST(A,B) A
#define TEST2(A,B) (A,B)
#define TEST3(A,B) TEST TEST2(A,B)
int main()
{
TEST3(Hello,World) //This will expand to TEST (Hello,World)
TEST (hello, World) // This will expand to hello
}
The TEST3 will expand to “TEST (Hello,World)“, but it won’t be expanded further using TEST definition. I initially thought it must be due to a space between TEST and TEST2(hello, world) in the TEST3 definition. But the plain invocation of TEST (hello, world) expands properly. Can someone explain what is happening here?
The word TEST in
#define TEST3(A,B) TEST TEST2(A,B)is not a function-like macro invocation because it is not followed by an open parenthesis. When the pre-processor is expandingTEST3(Hello, World), it encounters TEST, finds it is not an invocation of a function-like macro, and outputs it as text; then it processesTEST2(A, B)and that is a macro invocation, so it outputs the corresponding text, which is(Hello,World), and it continues the processing with the open parenthesis. The TEST is gone, never to be preprocessed again.See C preprocessor and concatenation for a full discussion of macro expansion with quotes from the standard. You might find How to make a char string from a C macro’s value of some help, too.