C语言 C99 布尔数据类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4767923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C99 boolean data type?
提问by Eonil
What's the C99 boolean data type and how to use it?
什么是 C99 布尔数据类型以及如何使用它?
回答by Prasoon Saurav
Include <stdbool.h>header
包含<stdbool.h>标题
#include <stdbool.h>
int main(void){
bool b = false;
}
Macros trueand falseexpand to 1and 0respectively.
宏true和false扩展到1和0分别。
Section 7.16Boolean type and values < stdbool.h >
部分7.16布尔类型和值< stdbool.h >
- 1 The header
<stdbool.h>de?nes four macros.- 2 The macro
- bool expands to _Bool.
- 3 The remaining three macros are suitable for use in #if preprocessing directives. They are
- true : which expands to the integer constant 1,
- false: which expands to the integer constant 0, and
- __bool_true_false_are_defined which expands to the integer constant 1.
- 4 Notwithstanding the provisions of 7.1.3, a program may unde?ne and perhaps then rede?ne the macros bool, true, and false.
- 1 标题定义了
<stdbool.h>四个宏。- 2 宏
- bool 扩展为 _Bool。
- 3 其余三个宏适用于#if 预处理指令。他们是
- true :扩展为整数常量 1,
- false:扩展为整数常量 0,并且
- __bool_true_false_are_defined 扩展为整数常量 1。
- 4 尽管有 7.1.3 的规定,程序可能会取消并重新定义宏 bool、true 和 false。
回答by evandrix
Please do check out the answer here on this related thread found on DaniWeb.
extracted and quoted here for convenient reference:-
在此摘录并引用以方便参考:-
usage of new keywords in c99
c99中新关键字的使用
_Bool: C99's boolean type. Using _Bool directly is only recommended if you're maintaining legacy code that already defines macros for bool, true, or false. Otherwise, those macros are standardized in the
<stdbool.h>header. Include that header and you can use bool just like you would in C++.
_Bool: C99 的布尔类型。仅当您维护已为 bool、true 或 false 定义宏的遗留代码时,才建议直接使用 _Bool。否则,这些宏在
<stdbool.h>标头中标准化。包含该头文件,您可以像在 C++ 中一样使用 bool。
#include <stdio.h>
#include <stdbool.h>
int main ( void )
{
bool b = true;
if ( b )
printf ( "Yes\n" );
else
printf ( "No\n" );
return 0;
}

