简体   繁体   中英

Macro with variable length of parameters

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

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

in C code

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

all invoke the above CALL macro. If ar2, ar3 not used, preprocessor just ignore the line with ar2 or ar3.

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

Key word is __VA_ARGS__ ( Variadic Macros ):

A macro can be declared to accept a variable number of arguments much as a function can. The syntax for defining the macro is similar to that of a function. Here is an example:

 #define eprintf(...) fprintf (stderr, __VA_ARGS__)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM