Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I implement a Variadic noop in C with evaluation

I want to use a noop macro in C (similar to msvc's __noop) that still evaluates its arguments.

example:

#ifdef _DEBUG
#define printf_debug(...) printf(__VA_ARGS__)
#else
#define printf_debug(...) NOOP(__VA_ARGS__)
#endif

int main()
{
     int i = 0;
     printf_debug("i: %i\n", i++);
     return i;//should return 1 when in release mode
}
like image 793
Badasahog Avatar asked Feb 01 '26 19:02

Badasahog


1 Answers

The simplest thing you can do is this:

#ifdef _DEBUG
#define printf_debug(...) printf(__VA_ARGS__)
#else
#define printf_debug(...) (__VA_ARGS__)
#endif

Although it would generate warning for unused values in a comma expression.

You could also add a noop function:

#ifdef _DEBUG
#define printf_debug(...) printf(__VA_ARGS__)
#else
int printf_noop(const char *p, ...) { (void )p; return 0; }
#define printf_debug(...) printf_noop(__VA_ARGS__)
#endif

This avoids the warnings but still generates a function call.

like image 184
dbush Avatar answered Feb 04 '26 09:02

dbush



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!