1. C语言没有bool类型,可以 typedef int bool 来替代,再 #define true 1 #define false 0
如果没有typdef,在用了bool的地方会出现,下面的错误提示(gcc):
error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
2. C99 包含了stdbool.h 直接包含就可已使用bool了.
一个实现如下:
/* stdbool.h -- Boolean type and values (substitute for missing C99 standard header) public-domain implementation from [EMAIL PROTECTED] implements subclause 7.16 of ISO/IEC 9899:1999 (E) */
#ifndef __bool_true_false_are_defined
#define __bool_true_false_are_defined 1
/* program is allowed to contain its own definitions, so ... */
#undef bool
#undef true
#undef false
#if __STDC_VERSION__ < 199901
typedef int _Bool
#endif
#define bool _Bool
#define true 1
#define false 0
#endif /* !defined(__bool_true_false_are_defined) */