Macro to turn off printf statements

后端 未结 10 1624
粉色の甜心
粉色の甜心 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

    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*, ...)
    

提交回复
热议问题