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
I have a feeling not because the preprocessor only does one pass.
Official gcc only. No third-party tools please.
The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation. It is called a macro processor because it allows you to define macros, which are brief abbreviations for longer constructs.
No, because C 2011 [N1570] 6.10. 3.4 3 says, about macro replacement, “The resulting completely macro-replaced preprocessing token sequence is not processed as a preprocessing directive even if it resembles one,…” Show activity on this post.
(2) However, you cannot define a macro of a macro like #define INCLUDE #define STDH include <stdio. h> .
This C tutorial explains how to use the #define preprocessor directive in the C language.
Macros can't expand into preprocessing directives. From C99 6.10.3.4/3 "Rescanning and further replacement":
The resulting completely macro-replaced preprocessing token sequence is not processed as a preprocessing directive even if it resembles one,
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With