C 没有任何内置的 boolean 类型。在 C 中使用它们的最佳方式是什么?
最佳答案
从好到坏:
选项 1(C99 和更新版本)
#include <stdbool.h>
选项 2
typedef enum { false, true } bool;
选项 3
typedef int bool;
enum { false, true };
选项 4
typedef int bool;
#define true 1
#define false 0
说明
- 选项 1 只有在您使用 C99(或更新版本)时才有效,并且这是执行此操作的“标准方法”。如果可能,请选择此项。
- 选项 2、3 和 4 实际上具有相同的行为。 #2 和 #3 不使用 #defines,但我认为这样更好。
如果您犹豫不决,请选择#1!
关于c - 在 C 中使用 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1921539/