使用 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 看到列舉