#define TRUE !FALSE vs #define TRUE 1

前端 未结 6 1483
粉色の甜心
粉色の甜心 2021-01-18 02:31

Putting aside the fact that since c99 the stdbool.h has existed, when defining macros to handle Boolean types in C is there any difference between the following

6条回答
  •  北海茫月
    2021-01-18 03:04

    They are just same.

    • !0 is 1 so !FALSE is 1

    #define TRUE !FALSE has no technical benefit at all although it existed for along time and appeared many where.

    #define TRUE !FALSE can be misunderstood, one could think that TRUE represents every value which not 0.

    • Only 1 equals to TRUE, others values like 2,3, 255... (which !=0) does not equal to TRUE

    To prevent this misunderstanding, many organizations require not using #define TRUE !FALSE any more or comparison to TRUE should be changed to !FALSE:

    // Should not     
    if (var_bool == TRUE) {
    ...
    }
    
    //Should    
    if (var_bool != FALSE) {
    ...
    }
    

提交回复
热议问题