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

前端 未结 6 1410
别跟我提以往
别跟我提以往 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:28

    Simply create a macro DEBUG_PRINT that does the actual printing:

    #define DEBUG_PRINT(n, str)    \
                                   \
      if(n == 1)                   \
      {                            \
        printf("%s", str);         \
      }                            \
      else if(n == 2)              \
      {                            \
        do_something_else();       \
      }                            \
                                   \
    #endif
    
    
    #include 
    
    int main()
    {
      DEBUG_PRINT(1, "testing");
    }
    

    If the macro isn't defined, then you will get a compiler error because the symbol is not recognized.

提交回复
热议问题