C macro: #if check for equality

前端 未结 3 1183
礼貌的吻别
礼貌的吻别 2020-12-16 10:03

Is there a way to do check for numerical equality in macros?

I want to do something like

#define choice 3

#if choice == 3
  ....
#endif

#if choice          


        
相关标签:
3条回答
  • 2020-12-16 10:35

    Another way to write your code uses chained #elif directives:

    #if choice == 3
      ...
    #elif choice == 4
      ...
    #else
      #error Unsupported choice setting
    #endif
    

    Note that if choice is not #defined, the compiler (preprocessor) treats it as having the value 0.

    0 讨论(0)
  • 2020-12-16 10:44

    As far as i know that should work. What compiler are you using ?

    PS : Just for information, the defines names are usually written in caps !

    #define CHOICE 3

    0 讨论(0)
  • 2020-12-16 10:45

    Indeed that should work. See http://gcc.gnu.org/onlinedocs/cpp/If.html#If

    That reference is accurate, but written in "standards format":  abstractly without examples.

    0 讨论(0)
提交回复
热议问题