Given macro definitions. What will be the output of the following program?
#include <iostream>

#define X Hello
#define Y world

#define _CONCAT(A, B) #A #B
#define CONCAT(A, B) _CONCAT(A, B)

int main() {
    std::cout << CONCAT(X, Y) << std::endl;
    std::cout << _CONCAT(X, Y) << std::endl;
    return 0;
}
Explanation
The point here is the order of expansion of macros. In the first case, the macro expansion works as expected, X and Y are replaced with hello world, the preprocessor expands all three macros(CONCAT, X and Y), in the expression CONCAT(X,Y), to _CONCAT(Hello, world) -> #Hello #world and finally to Helloworld
In the second case, the expansion of _CONCAT(X,Y), expands to #X #Y, hence the result.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes