C preprocessor Macro defining Macro

后端 未结 6 1210
耶瑟儿~
耶瑟儿~ 2020-12-29 01:49

Can you do something like this with a macro in C?

#define SUPERMACRO(X,Y) #define X Y

then

SUPERMACRO(A,B) expands to #define A B
<         


        
6条回答
  •  无人及你
    2020-12-29 01:54

    You cannot define macros in other macros, but you can call a macro from your macro, which can get you essentially the same results.

    #define B(x) do {printf("%d", (x)) }while(0)
    #define A(x) B(x)
    

    so, A(y) is expanded to do {printf("%d", (y)) }while(0)

提交回复
热议问题