极品_Bool在C99? [英] Need for _Bool in C99?
问题描述
我正在读一本关于C.
它说,C99增加了一个数据类型_Bool。
它基本上是一个int,但只存储0或1。
现在,我不明白为什么有必要这样的数据类型。
我们已经有了布尔含蓄地转化为int,反之亦然。
因此,有人可以告诉我的情况是这样的数据类型将是有益的。
PS:C ++似乎不支持这种数据类型所看到这里
的#include<&iostream的GT;
使用命名空间std;诠释主(){
//你的code到这里
_Bool B = FALSE;
如果(二== 0)
输出(假);
其他
的printf(TRUE);
返回0;
}
它说,C99加入了可变_Bool
块引用>没有,C99增加了一个内置的名为type
_Bool
,它可以有两种0
或值1
。头,< stdbool.h>
定义宏在布尔
扩展到_Bool
,假
到0
和真正
到1
。C ++,在另一方面,有一个内置的名为type
布尔
,它可以有值真正
和假
。出于兼容性考虑,C ++ 11指定stdbool.h
应该是present,而空。 (有些C编译器提供的C ++的布尔
作为扩展pre-C99)。与C99新增的意图是提供相同的设施,C ++,但是这并不能否定旧C89 code(一种方式,其中纯
布尔
是可作为名称)。在我看来,宏观的解决方案,他们想出了不太理想,而事实上它仍然是相当罕见请参阅C code,它使用布尔类型,而它们普遍在C ++中。I am reading a book on C. It says that C99 added a data type _Bool. It is basically an int but stores only 0 or 1. Now I do not understand why there is a need of such a data type. We already have bool which implicitly translates to int and vice versa. So can somebody please tell me a situation where such a data type would be useful.
PS: C++ does not seem to support such a data type as seen here.
#include <iostream> using namespace std; int main() { // your code goes here _Bool b = false; if(b == 0) printf("FALSE"); else printf("TRUE"); return 0; }
解决方案It says that C99 added a variable _Bool
No, C99 added a built-in type called
_Bool
, which can have values of either0
or1
. The header,<stdbool.h>
defines macros in whichbool
expands to_Bool
,false
to0
, andtrue
to1
.C++, on the other hand, has a built in type called
bool
, which can have values oftrue
andfalse
. For compatibility, C++11 specifies thatstdbool.h
should be present, but empty. (Some C compilers provided C++'sbool
as an extension pre-C99.)The intention with the C99 additions was to provide the same facilities as C++, but in a way that didn't invalidate old C89 code (where plain
bool
was available as a name). In my opinion the macro solution they came up with is less than ideal, and indeed it's still quite rare to see C code which uses the boolean types, whereas they are pervasive in C++.这篇关于极品_Bool在C99?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!