bool 是原生 C 类型吗? [英] Is bool a native C type?

查看:38
本文介绍了bool 是原生 C 类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到Linux内核代码使用bool,但我认为bool是C++类型.bool 是标准 C 扩展(例如 ISO C90)还是 GCC 扩展?

I've noticed that the Linux kernel code uses bool, but I thought that bool was a C++ type. Is bool a standard C extension (e.g., ISO C90) or a GCC extension?

推荐答案

bool 在当前的 C - C99 中存在,但在 C89/90 中不存在.

bool exists in the current C - C99, but not in C89/90.

在 C99 中,本机类型实际上称为 _Bool,而 bool 是在 stdbool.h 中定义的标准库宏(它预期解析_Bool)._Bool 类型的对象保存 0 或 1,而 truefalse 也是来自 stdbool.h 的宏.

In C99 the native type is actually called _Bool, while bool is a standard library macro defined in stdbool.h (which expectedly resolves to _Bool). Objects of type _Bool hold either 0 or 1, while true and false are also macros from stdbool.h.

请注意,顺便说一句,这意味着 C 预处理器会将 #if true 解释为 #if 0,除非包含 stdbool.h.同时,需要 C++ 预处理器将 true 本地识别为语言文字.

Note, BTW, that this implies that C preprocessor will interpret #if true as #if 0 unless stdbool.h is included. Meanwhile, C++ preprocessor is required to natively recognize true as a language literal.

这篇关于bool 是原生 C 类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆