Variable Parameter Macro

Is there a way to #define macro with variable length parameters?

 #define CALL(ar1, ar2, ar3) do something #endif 

in code C

 CALL(0); CALL(0,1); CALL(0,1,2) 

all calls invoke the CALL macro. If ar2, ar3 is not used, the preprocessor simply ignores the string with ar2 or ar3.

+4
source share
1 answer

Yes, take a look at this: http://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html

Keyword __VA_ARGS__ (Variadic Matrices):

You can declare a macro to accept a variable number of arguments, how it can function. The syntax for defining a macro is similar to the syntax of a function. Here is an example:

  #define eprintf(...) fprintf (stderr, __VA_ARGS__) 
+5
source

All Articles