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

前端 未结 8 1414
清歌不尽
清歌不尽 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.

    0 讨论(0)
  • 2020-12-12 12:37

    The pratical difference is none. 0 is evaluated to false and 1 is evaluated to true. The fact that you use a boolean expression (1 == 1) or 1, to define true, doesn't make any difference. They both gets evaluated to int.

    Notice that the C standard library provides a specific header for defining booleans: stdbool.h.

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