Search code examples
cgccmacrosvariadic

Swallowing comma in variadic macros on compilers that do not recognise ##


I need to write a variadic macro in C which must take zero or more arguments.

In gcc, that can be achieved by adding "##" after the comma, e.g. ,##____VA_ARGS____ as answered in Variadic macros with zero arguments.

However, the compiler in my build system (beyond my control) does not understand the ,## syntax and so does not swallow the comma.

Is there a workaround I can use?


Solution

  • Yes, gcc swallowing the comma is non standard and you should not rely on that.

    With C99 conforming preprocessors you may achieve a similar effect by testing for a macro arguments that is the empty token. For the ideas of how this works you could see here, for a whole set of preprocessor macros that ease the programming of such features there is P99.