C语言 定义一个宏以便DEBUG

执行结果截图:

 

    代码:

#include "stdio.h"
#define DEBUG
//#undef DEBUG
#define PRINT(format, ...) printf(# format, ##__VA_ARGS__)
// 定义一个宏连接两个参数
#define TOGETHER(x , y)  x ## y

static int DebugPrintf(const char * format, ...);
static int DebugPrintf(const char * format, ...)
{
// 如果定义了插桩调试宏DEBUG,则
#ifdef DEBUG
va_list argPtr;
int count;
// 获取可变参数列表
va_start(argPtr, format);
// 强制刷新输出缓冲区
fflush(stdout);

// fprintf(stderr, "%s %s [%d]: ", __FILE__, __FUNCTION__, __LINE__);

// 将信息输出到标准出错流设备
count = vfprintf(stderr, format, argPtr);
// 可变参数列表结束
va_end(argPtr);
#else
// 如果未定义插桩调试宏,则运行空函数体
do {

} while(0);
#endif
}

int main(void)
{
DebugPrintf("DebugPrintf -> %p\n", DebugPrintf);

PRINT(%d\n, TOGETHER(2, 50));

return 0;
}

 

posted @ 2022-01-08 00:07  JohnnyH  阅读(148)  评论(0)    收藏  举报