Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ##__VA_ARGS__ mean?

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?

like image 775
Mohammed Micro Avatar asked Oct 19 '18 11:10

Mohammed Micro


1 Answers

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

like image 184
Trass3r Avatar answered Oct 10 '22 01:10

Trass3r