asu*_*sex 248 c gcc boolean linux-kernel
我注意到Linux内核代码使用bool,但我认为bool是C++类型.bool是标准C扩展(例如,ISO C90)还是GCC扩展?
AnT*_*AnT 348
bool 存在于当前的C-C99中,但不存在于C89/90中.
在C99中,实际上调用了本机类型_Bool,而bool在stdbool.h其中定义了标准库宏(预期解析为_Bool).类型_Bool为0或1的对象,true而且false也是来自的宏stdbool.h.
注意,顺便说一句,这意味着C预处理器将解释#if true为#if 0除非stdbool.h包括在内.同时,C++预处理器需要本地识别true为语言文字.
Jos*_*ley 115
C99添加了内置_Bool数据类型(详见维基百科),如果你#include <stdbool.h>,它提供bool了一个宏_Bool.
你特别询问了Linux内核.它假设include/linux/types.h中存在_Bool并提供了booltypedef .
Bob*_*toe 32
不,boolISO C90中没有.
以下是标准C(不是C99)中的关键字列表:
autobreakcasecharconstcontinuedefaultdodoubleelseenumexternfloatforgotoifintlongregisterreturnshortsignedstaticstructswitchtypedefunionunsignedvoidvolatilewhile这篇文章讨论了内核和标准中使用的与C的其他一些差异:http://www.ibm.com/developerworks/linux/library/l-gcc-hacks/index.html
Rob*_*Rob 32
C99在stdbool.h中有它,但在C90中它必须被定义为typedef或enum:
typedef int bool;
#define TRUE 1
#define FALSE 0
bool f = FALSE;
if (f) { ... }
Run Code Online (Sandbox Code Playgroud)
或者:
typedef enum { FALSE, TRUE } boolean;
boolean b = FALSE;
if (b) { ... }
Run Code Online (Sandbox Code Playgroud)
小智 16
/* 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. */
Run Code Online (Sandbox Code Playgroud)
stdbool.h定义了宏true和false,但请记住它们被定义为 1 和 0。
这就是为什么sizeof(true)equals sizeof(int),对于 32 位架构来说是 4。