How to make a variadic macro (variable number of arguments)

CG++C PreprocessorVariadic

C Problem Overview


I want to write a macro in C that accepts any number of parameters, not a specific number

example:

#define macro( X )  something_complicated( whatever( X ) )

where X is any number of parameters

I need this because whatever is overloaded and can be called with 2 or 4 parameters.

I tried defining the macro twice, but the second definition overwrote the first one!

The compiler I'm working with is g++ (more specifically, mingw)

C Solutions


Solution 1 - C

C99 way, also supported by VC++ compiler.

#define FOO(fmt, ...) printf(fmt, ##__VA_ARGS__)

Solution 2 - C

__VA_ARGS__ is the standard way to do it. Don't use compiler-specific hacks if you don't have to.

I'm really annoyed that I can't comment on the original post. In any case, C++ is not a superset of C. It is really silly to compile your C code with a C++ compiler. Don't do what Donny Don't does.

Solution 3 - C

I don't think that's possible, you could fake it with double parens ... just as long you don't need the arguments individually.

#define macro(ARGS) some_complicated (whatever ARGS)
// ...
macro((a,b,c))
macro((d,e))

Solution 4 - C

#define DEBUG

#ifdef DEBUG
  #define PRINT print
#else
  #define PRINT(...) ((void)0) //strip out PRINT instructions from code
#endif 

void print(const char *fmt, ...) {

	va_list args;
	va_start(args, fmt);
	vsprintf(str, fmt, args);
        va_end(args);

        printf("%s\n", str);
	
}

int main() {
   PRINT("[%s %d, %d] Hello World", "March", 26, 2009);
   return 0;
}

If the compiler does not understand variadic macros, you can also strip out PRINT with either of the following:

#define PRINT //

or

#define PRINT if(0)print

The first comments out the PRINT instructions, the second prevents PRINT instruction because of a NULL if condition. If optimization is set, the compiler should strip out never executed instructions like: if(0) print("hello world"); or ((void)0);

Solution 5 - C

explained for g++ here, though it is part of C99 so should work for everyone

http://www.delorie.com/gnu/docs/gcc/gcc_44.html

quick example:

#define debug(format, args...) fprintf (stderr, format, args)

Solution 6 - C

• Variable number of arguments is denoted by an ellipsis (...) • The syntax of ISO C requires at least one fixed argument before the ‘...’

For example, you can type:

#define DEBUGMSG ( int, ...)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhasenView Question on Stackoverflow
Solution 1 - CAlex BView Answer on Stackoverflow
Solution 2 - CcmccabeView Answer on Stackoverflow
Solution 3 - CeduffyView Answer on Stackoverflow
Solution 4 - CSymonView Answer on Stackoverflow
Solution 5 - CDarenWView Answer on Stackoverflow
Solution 6 - CKateridzheView Answer on Stackoverflow