I would like to know what ##
does in this macro definition:
#define debug(M, ...) fprintf(stderr,M "\n",##__VA_ARGS __)
I googled for an answer and I came up with the following.
The ##
will remove the comma if no variable arguments are given to the macro. So, if the macro is invoked like this
debug("message");
with no quotes, it is expanded to
fprintf(stderr,"message");
not
fprintf(stderr,"message",);
Why is the comma removed?
It's a non-portable syntax introduced by gcc to specifically deal with this corner case of passing no arguments at all. Without the ## it would complain about the trailing comma being a syntax error.
https://gcc.gnu.org/onlinedocs/gcc/Variadic-Macros.html
C++20 introduced __VA_OPT__
for this purpose:
https://en.cppreference.com/w/cpp/preprocessor/replace
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