简体   繁体   中英

passing … parameters at DEFINE macro in C

#define MY_PRINT(_format, ...) printf("MIME : %s" _format, __FUNCTION__, __VA_ARGS__);

and I'd like to add "\\n " at the end of print message.

But,

#define MY_PRINT_LN(_format, ...) MY_PRINT(_format "\n", ...)

has compile error. how to pass ... parameter at define macro?

You use __VA_ARGS__ :

#define MY_PRINT_LN(_format, ...) MY_PRINT(_format "\n", __VA_ARGS__)

This is standardized as of C99.

If there are no arguments, you'll have a redundant comma at the end, though. There's no standard solution, but GCC (and possibly other compilers) offers an extension:

#define MY_PRINT_LN(_format, ...) MY_PRINT(_format "\n", ## __VA_ARGS__)

With the extra ## , the final comma is removed if there are no arguments.

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