Is comparing a BOOL against YES dangerous?

后端 未结 4 2101
轻奢々
轻奢々 2020-12-15 12:29

I found a comment today in a source file:

//  - no longer compare BOOL against YES (dangerous!)

Is comparing BOOL against

4条回答
  •  星月不相逢
    2020-12-15 13:19

    The problem is that BOOL is not a native type, but a typedef:

    typedef signed char      BOOL;
    
    #define YES             (BOOL)1
    #define NO              (BOOL)0
    

    As a char, its values aren't constrained to TRUE and FALSE. What happens with another value?

    BOOL b = 42;
    if (b)
    {
        // true
    }
    if (b != YES)
    {
        // also true
    }
    

提交回复
热议问题