如何定义不定参数的宏 - 小众知识

如何定义不定参数的宏

2015-11-04 10:38:49 苏内容
  标签: /参数/define
阅读:3225
大家都知道printf是不定参数的函数,那是否有不定参数的宏呢?可以!在最近工作中学要把一些debug信息打印出来,想定义一个宏printf_debug来做这件事情。当时搜索的两篇文章忘了复制下来,可以去搜索一下看看原理更容易理解。

#if (PRINTF_DEBUG_MSG == NU_TRUE)
#if (C99_SUPPORT == NU_TRUE)
#define printf_debug(...) printf_uart(__VA_ARGS__)
/* usage : printf_debug("%s.%d", "debug", 1); */
#else
#define printf_debug(args) (printf_uart args)
/* usage : printf_debug(("%s.%d", "debug", 1)); (Note : Double parentheses)*/
#endif
#else
#define printf_debug(...)
#endif

函数为:

void printf_uart(const char *format, ...)
{
    va_list arg;
    char tmp[255];
    va_start(arg, format); 
    vsprintf(tmp, format, arg); 
    va_end(arg); 
    uart_write(DATA_PORT, tmp, strlen(tmp));
}

扩展阅读
相关阅读
© CopyRight 2010-2021, PREDREAM.ORG, Inc.All Rights Reserved. 京ICP备13045924号-1