customized print macro/##__VAR_ARGS__

customized print macro

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

then you can use DEBUG_INFO() macro to print debug info like printf.

example:

DEBUG_INFO("name: %s, age: %d ", "hello world!", 26);

notice:

a. __VA_ARGS__ is pre-defined macro in C.

b. DEBUG_INFO(fmt, ...), the ... in this macro definition represents(can only represent) the last argument of this macro.

the below form is wrong:

#define DEBUG_INFO(fmt, ..., A)

c. the below form is also correct:

#define DEBUG_INFO(fmt, var_args...)  printf(fmt, ##var_args)

2. ##__VAR_ARGS__

#define CUSTOM_PRINT(fmt, ...)  printf(fmt, ##__VAR_ARGS__)

a. the ## before __VAR_ARGS__ is used to tell compiler to remove the comma before ##__VAR_ARGS__ under the variable argument list is null to avoid complile error(unnecessary comma before __VAR_ARGS__) under this case.

原文地址:https://www.cnblogs.com/aspirs/p/7111566.html