Preprocessor macro and BOOL weirdness

后端 未结 2 1772

Code below yields the output \"yes defined\", \"no defined\" and \"yes\". Why?

#define FOOBAR NO
- (BOOL)application:(UIApplication *)application didFinishLa         


        
相关标签:
2条回答
  • 2021-01-13 16:36

    All identifieres that the preprocessor doesn't know of are replaced with 0 for evaluation in #if directives. If you don't have defined YES and NO both are 0 (and thus equal).

    0 讨论(0)
  • 2021-01-13 16:50

    What is the value of NO? If it's undefined (like YES), they will both evaluate to 0.

    This means your expression is essentially

    #if 0 == 0
    

    which is of course true, and thus causes the first call to be compiled.

    UPDATE: Not sure how BOOL is defined, but casting to what might be a typedef:ed type is not a very good idea when dealing with the preprocessor. Remember that the the #if is evaluated by the preprocessor, not by the compiler. Read something like this for more information about expressions in the preprocessor. Especially:

    The preprocessor does not know anything about types in the language.

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