使用真实和用C假

使用真实和用C假

问题描述:


  1. 与布尔类型,然后从使用真假

  2. 使用preprocessor定义的#define FALSE 0 ...的#define TRUE!(FALSE)

  3. 只需直接使用常量,即1和0

是我错过了还有其他的方法?什么是不同方法的利弊?

are there other methods I missed? What are the pros and cons of the different methods?

我想最快的将是3号,2更容易阅读依然(虽然按位求反将略有增加开销),1是最可读不是所有的编译器兼容。

I suppose the fastest would be number 3, 2 is more easily readable still (although bitwise negation will slightly add to overhead), 1 is most readable not compatible with all compilers.

就包括< stdbool.h> 如果您的系统提供了它。这定义了许多宏,包括布尔真正(分别定义为 _Bool ,0和1)。见C99第7.16更多的细节。

Just include <stdbool.h> if your system provides it. That defines a number of macros, including bool, false, and true (defined to _Bool, 0, and 1 respectively). See section 7.16 of C99 for more details.