下面是来自 cprogramming.com 的 stdbool.h 的定义:
#ifndef STDBOOL_H_
#define STDBOOL_H_
/**
* stdbool.h - ISO C99 Boolean type
* Author - Bill Chatfield
* E-mail - bill underscore chatfield at yahoo dot com
* Copyright - You are free to use for any purpose except illegal acts
* Warrenty - None: don't blame me if it breaks something
*
* In ISO C99, stdbool.h is a standard header and _Bool is a keyword, but
* some compilers don't offer these yet. This header file is an
* implementation of the standard ISO C99 stdbool.h header file. It checks
* for various compiler versions and defines things that are missing in
* those versions.
*
* The GNU and Watcom compilers include a stdbool.h, but the Borland
* C/C++ 5.5.1 compiler and the Microsoft compilers do not.
*
* See http://predef.sourceforge.net/precomp.html for compile macros.
*/
/**
* Borland C++ 5.5.1 does not define _Bool.
*/
#ifdef __BORLANDC__
typedef int _Bool;
#endif
/**
* Microsoft C/C++ version 14.00.50727.762, which comes with Visual C++ 2005,
* and version 15.00.30729.01, which comes with Visual C++ 2008, do not
* define _Bool.
*/
#if defined(_MSC_VER) && _MSC_VER <= 1500
typedef int _Bool;
#endif
/**
* Define the Boolean macros only if they are not already defined.
*/
#ifndef __bool_true_false_are_defined
#define bool _Bool
#define false 0
#define true 1
#define __bool_true_false_are_defined 1
#endif
#endif /*STDBOOL_H_*/
但是上述文件在 Visual Studio 2012 中不工作。
下面是我自己写的 stdbool.h 的定义:
#ifndef STDBOOL_H_
#define STDBOOL_H_
/**
* stdbool.h
* Author - Yaping Xin
* E-mail - xinyp at live dot com
* Copyright - You are free to use for any purpose except illegal acts
* Warrenty - None: don't blame me if it breaks something
*
* In ISO C99, stdbool.h is a standard header and _Bool is a keyword, but
* some compilers don't offer these yet. This header file is an
* implementation of the stdbool.h header file.
*
*/
#ifndef _Bool
typedef unsigned char _Bool;
#endif /* _Bool */
/**
* Define the Boolean macros only if they are not already defined.
*/
#ifndef __bool_true_false_are_defined
#define bool _Bool
#define false 0
#define true 1
#define __bool_true_false_are_defined 1
#endif /* __bool_true_false_are_defined */
#endif /* STDBOOL_H_ */
参考链接:
- stdbool.h in Visual Studio C++ Express 2008 ( standard C code) http://cboard.cprogramming.com/c-programming/138189-stdbool-h-visual-studio-cplusplus-express-2008-standard-c-code.html
- Boolean data type http://en.wikipedia.org/wiki/Boolean_data_type#C99
- Where is stdbool.h? http://stackoverflow.com/questions/1656874/where-is-stdbool-h