我注意到Linux内核代码使用bool类型,但我以为bool类型是c++类型。bool是标准的C扩展名(例如,ISO C90)还是GCC扩展名?
当前回答
C99在stdbol .h中有它,但在C90中必须定义为类型定义或enum:
typedef int bool;
#define TRUE 1
#define FALSE 0
bool f = FALSE;
if (f) { ... }
另外:
typedef enum { FALSE, TRUE } boolean;
boolean b = FALSE;
if (b) { ... }
其他回答
/* Many years ago, when the earth was still cooling, we used this: */
typedef enum
{
false = ( 1 == 0 ),
true = ( ! false )
} bool;
/* It has always worked for me. */
C99在stdbool.h中定义了bool, true和false。
h定义了true和false宏,但是请记住它们被定义为1和0。
这就是sizeof(true) = sizeof(int)的原因,对于32位架构,sizeof(int)为4。
从C23开始,bool、true和false都是C关键字,不需要任何#include。
Bool成为基本的内置数据类型之一。
_Bool仍然有效,并被视为“替代拼写”。
头文件<stdbool.h>只提供过时的宏__bool_true_false_are_defined,它展开为整数常量1。
你可以在这里找到最新的草案: https://open-std.org/JTC1/SC22/WG14/www/docs/n2912.pdf
C99在stdbol .h中有它,但在C90中必须定义为类型定义或enum:
typedef int bool;
#define TRUE 1
#define FALSE 0
bool f = FALSE;
if (f) { ... }
另外:
typedef enum { FALSE, TRUE } boolean;
boolean b = FALSE;
if (b) { ... }