Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: Preprocessor in Macros?

Is there a way to use preprocessor keywords inside of a macro? If there is some sort of escape character or something, I am not aware of it.

For example, I want to make a macro that expands to this:

#ifdef DEBUG
    printf("FOO%s","BAR");
#else
    log("FOO%s","BAR");
#endif

from this:

PRINT("FOO%s","BAR");

Is this possible, or am I just crazy (and I will have to type out the preprocessor conditional every time I want to show a debug message)?

like image 441
Matt Bell Avatar asked Dec 28 '25 16:12

Matt Bell


1 Answers

You can't do that directly, no, but you can define the PRINT macro differently depending on whether DEBUG is defined:

#ifdef DEBUG
    #define PRINT(...) printf(__VA_ARGS__)
#else 
    #define PRINT(...) log(__VA_ARGS__)
#endif
like image 159
James McNellis Avatar answered Dec 30 '25 07:12

James McNellis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!