#define macro for debug printing in C?

后端 未结 12 1676
夕颜
夕颜 2020-11-21 23:59

Trying to create a macro which can be used for print debug messages when DEBUG is defined, like the following pseudo code:

#define DEBUG 1
#define debug_prin         


        
12条回答
  •  梦谈多话
    2020-11-22 00:23

    Here's the version I use:

    #ifdef NDEBUG
    #define Dprintf(FORMAT, ...) ((void)0)
    #define Dputs(MSG) ((void)0)
    #else
    #define Dprintf(FORMAT, ...) \
        fprintf(stderr, "%s() in %s, line %i: " FORMAT "\n", \
            __func__, __FILE__, __LINE__, __VA_ARGS__)
    #define Dputs(MSG) Dprintf("%s", MSG)
    #endif
    

提交回复
热议问题