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\n", "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.

 

posted @ 2017-07-03 15:59  aspirs  阅读(345)  评论(0编辑  收藏  举报