Is there a way to both check a macro is defined and it equals a certain value at the same time

前端 未结 6 1391
别跟我提以往
别跟我提以往 2020-12-15 17:39

I regularly use object-like preprocessor macros as boolean flags in C code to turn on and off sections of code.

For example

#define          


        
6条回答
  •  [愿得一人]
    2020-12-15 18:12

    Yes you can check both:

    #if defined DEBUG  &&  DEBUG == 1
    #  define D(...) printf(__VA_ARGS__)
    #else
    #  define D(...)
    #endif
    

    In this example even when #define DEBUG 0 but it is not equal to 1 thus nothing will be printed.

    You can do even this:

    #if defined DEBUG  &&  DEBUG
    #  define D(...) printf(__VA_ARGS__)
    #else
    #  define D(...)
    #endif
    

    Here if you #define DEBUG 0 and then D(1,2,3) also nothing will be printed

    DOC

提交回复
热议问题