极品_Bool在C99? [英] Need for _Bool in C99?

查看:176
本文介绍了极品_Bool在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 either 0 or 1. The header, <stdbool.h> defines macros in which bool expands to _Bool, false to 0, and true to 1.

C++, on the other hand, has a built in type called bool, which can have values of true and false. For compatibility, C++11 specifies that stdbool.h should be present, but empty. (Some C compilers provided C++'s bool 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屋!

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