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

前端 未结 8 1430
清歌不尽
清歌不尽 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:36

    We don't know the exact value that TRUE is equal to and the compilers can have their own definitions. So what you privode is to use the compiler's internal one for definition. This is not always necessary if you have good programming habits but can avoid problems for some bad coding style, for example:

    if ( (a > b) == TRUE)

    This could be a disaster if you mannually define TRUE as 1, while the internal value of TRUE is another one.

提交回复
热议问题