Why #define TRUE (1==1) in a C boolean macro instead of simply as 1?

前端 未结 8 1415
清歌不尽
清歌不尽 2020-12-12 11:55

I\'ve seen definitions in C

#define TRUE (1==1)
#define FALSE (!TRUE)

Is this necessary? What\'s the benefit over simply defining TRUE as

8条回答
  •  失恋的感觉
    2020-12-12 12:30

    This approach will use the actual boolean type (and resolve to true and false) if the compiler supports it. (specifically, C++)

    However, it would be better to check whether C++ is in use (via the __cplusplus macro) and actually use true and false.

    In a C compiler, this is equivalent to 0 and 1.
    (note that removing the parentheses will break that due to order of operations)

提交回复
热议问题