Using true and false in C

后端 未结 15 1902
情书的邮戳
情书的邮戳 2020-12-01 01:52

As far as I can see there are 3 ways to use booleans in c

  1. with the bool type, from then using true and false
  2. defining using preprocessor #defin
15条回答
  •  青春惊慌失措
    2020-12-01 02:40

    Whichever of the three you go with, compare your variables against FALSE, or false.

    Historically it is a bad idea to compare anything to true (1) in c or c++. Only false is guaranteed to be zero (0). True is any other value.   Many compiler vendors have these definitions somewhere in their headers.  

    #define TRUE 1
    #define FALSE 0
    

    This has led too many people down the garden path.   Many library functions besides chartype return nonzero values not equal to 1 on success. There is a great deal of legacy code out there with the same behavior.

提交回复
热议问题