Macro to turn off printf statements

后端 未结 10 1596
粉色の甜心
粉色の甜心 2020-12-14 08:42

What MACRO can be used to switch off printf statements, rather than removing them all for deployment builds, I just want to switch them off, skip them, ignore them.

相关标签:
10条回答
  • 2020-12-14 09:02
    #ifdef IGNORE_PRINTF
    #define printf(fmt, ...) (0)
    #endif
    

    See also C #define macro for debug printing which discusses some important issues closely related to this.

    0 讨论(0)
  • 2020-12-14 09:02

    If you want to avoid the potential warning that Jonathan's answer may give you and if you don't mind an empty call to printf you could also do something like

    #define printf(...) printf("")
    

    This works because C macros are not recursive. The expanded printf("") will just be left as such.

    Another variant (since you are using gcc) would be something like

    inline int ignore_printf(char const*, ...) 
             __attribute__ ((format (printf, 1, 2)));
    inline int ignore_printf(char const*, ...) { return 0; }
    
    #define printf ignore_printf
    

    and in one compilation unit

    int ignore_printf(char const*, ...)
    
    0 讨论(0)
  • 2020-12-14 09:04

    Use this macro to enable or disable the printf.

    //Uncomment the following line to enable the printf function.
    //#define ENABLE_PRINTF
    #ifdef ENABLE_PRINTF
        #define    DEBUG_PRINTF(f,...)    printf(f,##__VA_ARGS__)
    #else
        #define    DEBUG_PRINTF(f,...)
    #endif
    

    Then call "DEBUG_PRINTF" instead of "printf".

    For example:

    DEBUG_PRINTF("Hello world: %d", whateverCount);
    
    0 讨论(0)
  • 2020-12-14 09:08

    Below simple function serves the purpose, I use the same.

    int printf(const char *fmt, ...)
    {
    return (0)
    }
    
    0 讨论(0)
提交回复
热议问题