使用 typedef 定义 bool 类型

考虑到大多数调试器都不知道 #define 宏,但可以检查 enum 常量,可能需要这样做:

#if __STDC_VERSION__ < 199900L
typedef enum { false, true } bool;
/* Modern C code might expect these to be macros. */
# ifndef bool
#  define bool bool
# endif
# ifndef true
#  define true true
# endif
# ifndef false
#  define false false
# endif
#else
# include <stdbool.h>
#endif

/* Somewhere later in the code ... */
bool b = true;

这允许 C 的历史版本的编译器起作用,但如果使用现代 C 编译器编译代码,则保持向前兼容。

有关 typedef 的更多信息,请参阅的 typedef ,以获得更多关于 enum 看到枚举