2016年7月9日 星期六

C語言 - 使用Macro打印除錯訊息

#include <stdio.h>

#define DEBUG

#ifdef DEBUG
#define dprintf(fmt, arg...) \
do { \
        if (debug) \
                printf("DEBUG: " fmt, ##arg); \
        } while (0)
#else
#define dprintf(fmt, arg...)
#endif

int debug = 1;
int main(int argc, char *argv[])
{
        dprintf("%s\n", "hello world");
 
        return 0;
}

執行結果
DEBUG: hello world

沒有留言:

張貼留言