On executing the following code using ‘Macro Definitions Set 1’, I encounter the error “C2065: ‘C1’ : undeclared identifier”.
While using ‘Macro Definitions Set 2’, the code runs to give an output of 2.
I am guessing it has something to do with preprocessor tokenization. Please Explain.
#include <iostream>
// Macro Definitions Set 1
#define A 1
#define D(n) B(n)
#define B(n) C##n
#define CA 2
// Macro Definitions Set 2
//#define A 1
//#define D(n) C##n
//#define CA 2
int main ()
{
printf ("%d", D(A));
}
The macro parameter is substituted unless it is before or after
##, or after#.So in Set 1,
D(A)becomesB(1)after substitution ofD(n), which is substituted toC1after rescan.In Set 2,
D(A)becomesCA, whereAis not substituted as it is after##, andCAbecomes2after rescan.