Sungguk Lim
Sungguk Lim

Reputation: 6228

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?

Upvotes: 0

Views: 110

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477040

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.

Upvotes: 4

Related Questions