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
并提供了bool
typedef .
Bob*_*toe 32
不,bool
ISO C90中没有.
以下是标准C(不是C99)中的关键字列表:
auto
break
case
char
const
continue
default
do
double
else
enum
extern
float
for
goto
if
int
long
register
return
short
signed
static
struct
switch
typedef
union
unsigned
void
volatile
while
这篇文章讨论了内核和标准中使用的与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。
归档时间: |
|
查看次数: |
307083 次 |
最近记录: |