C中缺少布尔数据类型 [英] The lack of a boolean data type in C

查看:67
本文介绍了C中缺少布尔数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先:我爱C并认为它很漂亮。但是,至少有一个主要缺陷是:b $ b:缺少布尔类型。


好​​的。你们中的一些人可能会参考C99及其_Bool(无论如何,

大写''B'是什么?)以及你可以包含的标题(显然)到

得到一个真实的bool。然而,这不是我的观点 - 从一开始就应该是




char是一个小int。我们都知道。但是,char some_bool = 0;

只是感觉不对,我认为大多数人都同意。另外,它的价格仍然太大了。


" int some_bool = 0;"是我 - 以及其他所有人,我认为 - 使用

作为bools。但是int是一个非常大的数据类型,它只会是
只有真或假(1或0)。这真的,真的让我感到烦恼。


为什么,当C设计时,他们没有看到建立一个

布尔类型的理由语言?现在重要的是,我想,但是回到

然后,应该有非常强大的技术原因。它只是

对我来说没有任何意义。


我已经问了很多人这个问题很长一段时间了,他们是/>
所有这些只是告诉我,我愚蠢地提起它。为什么?这不是我需要一个bool才能完成任何事情 - 这是原则。保存

资源和编码更美好的是一个Good Thing(TM)IMO。


所以...有人可以正确解释这个给我一次对全部?我是

确定必须有一个合乎逻辑的解释,似乎没有人真正了解b
。疯狂必须结束。


bool some_bool = 0; / *它有多棒...... * /

First of all: I love C and think that it''s beautiful. However, there is
at least one MAJOR flaw: the lack of a boolean type.

OK. Some of you might refer to C99 and its _Bool (what''s up with the
uppercase ''B'' anyway?) and the header you can include (apparently) to
get a real "bool". This isn''t my point, however -- it should have been
there from the beginning.

char is a small int. We all know that. However, "char some_bool = 0;"
simply feels wrong, and I think that most of you agree. Plus, it''s
still too large.

"int some_bool = 0;" is what I -- and everyone else, I assume -- use
for bools. But an int is a very large data type for something that will
only ever be true or false (1 or 0). This really, really bugs me.

Why, back when C was designed, didn''t they see a reason to build in a
boolean type into the language? Now it matters less, I guess, but back
then, there should have been very strong technical reasons. It just
doesn''t make any sense whatsoever to me.

I have asked many people about this for quite some time, and they are
all just telling me that I''m silly for bringing it up. Why? It''s not
that I NEED a bool to get anything done -- it''s the principle. Saving
resources and coding a little more prettily is a Good Thing (TM) IMO.

So... can somebody properly explain this to me once and for all? I''m
sure there MUST be a logical explanation that nobody seems to really
understand. The madness must end.

bool some_bool = 0; /* How great it would be... */

推荐答案

KimmoA写了

(文章中)

< 11 ********************* @ i12g2000cwa.googlegroups.com>):
KimmoA wrote
(in article
<11*********************@i12g2000cwa.googlegroups. com>):

首先:我爱C并认为它很漂亮。但是,至少有一个主要缺陷是:
:缺少布尔类型。
First of all: I love C and think that it''s beautiful. However, there is
at least one MAJOR flaw: the lack of a boolean type.



没有问题,哦,30年?

hasn''t been a problem for, oh, 30 years?


好​​的。你们中的一些人可能会参考C99及其_Bool(无论如何,

大写''B'是什么?)以及你可以包含的标题(显然)到

得到一个真实的bool。
OK. Some of you might refer to C99 and its _Bool (what''s up with the
uppercase ''B'' anyway?) and the header you can include (apparently) to
get a real "bool".



不,实际上我不喜欢任何有领先_字符的东西。

No, actually I don''t prefer anything with leading _ chars in it.


这不是我的意思,但是从一开始就应该是


This isn''t my point, however -- it should have been
there from the beginning.



不是真的需要。

Not really needed.


char是一个小int。我们都知道。但是,char some_bool = 0;

只是感觉不对,我认为大多数人都同意。另外,它的价格仍然太大了。
char is a small int. We all know that. However, "char some_bool = 0;"
simply feels wrong, and I think that most of you agree. Plus, it''s
still too large.



使用typedef让自己迷惑,让自己感觉更好。 :-) as

对于大型,在什么平台上它太大了?

有人真的需要数以百计的漂浮吗?如果是这样,

将它们打包到位域。

Use a typedef to confuse yourself so you feel better. :-) As
for being to large, on what platform is it too large? Does
anyone really need hundreds of them floating around? If so,
pack them into bitfields.


" int some_bool = 0;"是我 - 以及其他所有人,我认为 - 使用

作为bools。但是int是一个非常大的数据类型,它只会是
只有真或假(1或0)。这真的,真的让我烦恼。
"int some_bool = 0;" is what I -- and everyone else, I assume -- use
for bools. But an int is a very large data type for something that will
only ever be true or false (1 or 0). This really, really bugs me.



然后不要使用int。

Then don''t use int.


为什么,在设计C时回来,没有'他们认为有理由在语言中建立一个

布尔类型吗?现在重要的是,我想,但是回到

然后,应该有非常强大的技术原因。只是

对我来说没有任何意义。
Why, back when C was designed, didn''t they see a reason to build in a
boolean type into the language? Now it matters less, I guess, but back
then, there should have been very strong technical reasons. It just
doesn''t make any sense whatsoever to me.



几十年的程序员证明,这不是必需的。

-

Randy Howard( 2请删除FOOBAR)

精确观察的力量被那些没有得到它的人称为玩世不恭。 - George Bernard Shaw

It wasn''t needed, as decades of programmers demonstrated.
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw


" KimmoA" < ki **** @ gmail.comwrites:
"KimmoA" <ki****@gmail.comwrites:

首先:我爱C并认为它很美。但是,至少有一个主要缺陷是:b $ b:缺少布尔类型。


好​​的。你们中的一些人可能会参考C99及其_Bool(无论如何,

大写''B'是什么?)以及你可以包含的标题(显然)到

得到一个真实的bool。然而,这不是我的观点 - 它应该从一开始就是


First of all: I love C and think that it''s beautiful. However, there is
at least one MAJOR flaw: the lack of a boolean type.

OK. Some of you might refer to C99 and its _Bool (what''s up with the
uppercase ''B'' anyway?) and the header you can include (apparently) to
get a real "bool". This isn''t my point, however -- it should have been
there from the beginning.



好​​的 - 但它不是。我们无法追溯修复它。


至于名称_Bool,它被选中以避免与现有用户中的任何

标识符发生冲突码。有很多C90代码

使用bool作为标识符,如:

typedef enum {false,true} bool;

所以制作" bool"关键字会破坏任何此类代码。


以下划线和大写字母开头的标识符是

保留,因此新标准可以免费使用这是出于自己的目的。


当C首次被设计时,有人认为如果值为零,则将表达式

视为假,并且如果他们的价值是非零的,那么就是真的,这已经足够了。 (很多人仍然有这种感觉。)

Ok -- but it wasn''t. We can''t retroactively fix it.

As for the name "_Bool", it was chosen to avoid colliding with any
identifiers in existing user code. There''s plenty of C90 code that
uses "bool" as an identifier, as in:
typedef enum { false, true } bool;
so making "bool" a keyword would have broken any such code.

An identifier starting with an underscore and an uppercase letter is
reserved, so the new standard was free to use it for its own purposes.

When C was first being designed, it was felt that having expressions
treated as false if their value is zero, and as true if their value is
non-zero, was good enough. (A lot of people still feel that way.)


char是一个小int。我们都知道。但是,char some_bool = 0;

只是感觉不对,我认为大多数人都同意。另外,它的价格仍然太大了。
char is a small int. We all know that. However, "char some_bool = 0;"
simply feels wrong, and I think that most of you agree. Plus, it''s
still too large.



char是除了位字段以外的最小可能对象类型。

类型_Bool的大小至少为一个字节;它甚至可能更大。


假设C有一个内置布尔类型,大小为1位。

是该语言中唯一的这种类型(除了位字段)。以

为例,这样一个对象的地址会产生相当大的问题。在

许多系统中,从

字节中提取单个相关位的代码或包含此类对象的单词将远远超过
$ b $中的节省b数据大小。最有可能编译器会添加填充,所以你

不会保存任何东西。

char is the smallest possible object type other than a bit field.
Type _Bool has a size of at least one byte; it may even be bigger.

Suppose C had a built-in boolean type with a size of 1 bit. It would
be the only such type in the language (apart from bit fields). Taking
the address of such an object would create considerable problems. On
many systems, the code to extract the single relevant bit from the
byte or word containing such an object would far exceed the savings in
data size. Most likely the compiler would add padding anyway, so you
wouldn''t save anything.


" int some_bool = 0;" ;是我 - 以及其他所有人,我认为 - 使用

作为bools。但是int是一个非常大的数据类型,它只会是
只有真或假(1或0)。这真的,真的让我烦恼。
"int some_bool = 0;" is what I -- and everyone else, I assume -- use
for bools. But an int is a very large data type for something that will
only ever be true or false (1 or 0). This really, really bugs me.



我建议克服它。单个int并不是那么大。如果

大小困扰你,请使用char;它同样能够持有

值0和1.如果你想持有大量的布尔值,每个元素一个

位,有办法通过按位操作来做(我认为
同意对此更直接的方法会很好,但是那里没有足够的需求来证明它是合理的将它添加到

语言中。


[snip]

I suggest getting over it. A single int isn''t all that big. If the
size bothers you, use a char; it''s equally capable of holding the
values 0 and 1. If you want to hold a large array of booleans, one
bit per element, there are ways to do it with bitwise operations (I
agree that a more direct approach to this would be nice, but there
hasn''t been enough of a demand for it to justify adding it to the
language).

[snip]


所以......有人可以一劳永逸地向我解释这个吗?我是

确定必须有一个合乎逻辑的解释,似乎没有人真正了解b
。疯狂必须结束。
So... can somebody properly explain this to me once and for all? I''m
sure there MUST be a logical explanation that nobody seems to really
understand. The madness must end.



疯狂已经结束,假设您可以找到一个编译器,即
实现C99的那部分。

The madness has ended, assuming you can find a compiler that
implements that portion of C99.


bool some_bool = 0; / *它有多棒...... * /
bool some_bool = 0; /* How great it would be... */



#include< stdbool.h>


bool some_bool = false; / *你的问题又是什么? * /


如果你的实现不支持_Bool或< stdbool.h>,那么你可以定义自己的布尔类型。例如:


typedef enum {false,true} bool;


-

Keith Thompson(The_Other_Keith ) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

#include <stdbool.h>

bool some_bool = false; /* What was your problem, again? */

And if your implementation doesn''t support _Bool or <stdbool.h>, you
can always define your own Boolean type; for example:

typedef enum { false, true } bool;

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


KimmoA写道:
KimmoA wrote:

首先:我爱C并思考这很美。但是,至少有一个主要缺陷是:b $ b:缺少布尔类型。


好​​的。你们中的一些人可能会参考C99及其_Bool(无论如何,

大写''B'是什么?)以及你可以包含的标题(显然)到

得到一个真实的bool。然而,这不是我的观点 - 从一开始就应该是




char是一个小int。我们都知道。但是,char some_bool = 0;

只是感觉不对,我认为大多数人都同意。另外,它的价格仍然太大了。
First of all: I love C and think that it''s beautiful. However, there is
at least one MAJOR flaw: the lack of a boolean type.

OK. Some of you might refer to C99 and its _Bool (what''s up with the
uppercase ''B'' anyway?) and the header you can include (apparently) to
get a real "bool". This isn''t my point, however -- it should have been
there from the beginning.

char is a small int. We all know that. However, "char some_bool = 0;"
simply feels wrong, and I think that most of you agree. Plus, it''s
still too large.



我觉得不错。另外你总是可以写

typedef char bool;

It doesn''t feel wrong to me. Plus you can always write
typedef char bool ;


" int some_bool = 0;"是我 - 以及其他所有人,我认为 - 使用

作为bools。但是int是一个非常大的数据类型,它只会是
只有真或假(1或0)。这真的,真的让我感到烦恼。


为什么,当C设计时,他们没有看到建立一个

布尔类型的理由语言?现在重要的是,我想,但是回到

然后,应该有非常强大的技术原因。只是

对我来说没有任何意义。
"int some_bool = 0;" is what I -- and everyone else, I assume -- use
for bools. But an int is a very large data type for something that will
only ever be true or false (1 or 0). This really, really bugs me.

Why, back when C was designed, didn''t they see a reason to build in a
boolean type into the language? Now it matters less, I guess, but back
then, there should have been very strong technical reasons. It just
doesn''t make any sense whatsoever to me.



我假设因为编译器需要很小所以

没有理由添加一个可以模拟的类型

轻松使用已有的语言。

I assume because the compiler needed to be small so there
was no reason to add a type which could be emulated very
easily with what the language already had available.


我已经问了很多人这个问题很长一段时间了,他们是

所有这些只是告诉我,我愚蠢地提起它。为什么?这不是我需要一个bool才能完成任何事情 - 这是原则。节省

资源和更好的编码是Good Thing(TM)IMO。
I have asked many people about this for quite some time, and they are
all just telling me that I''m silly for bringing it up. Why? It''s not
that I NEED a bool to get anything done -- it''s the principle. Saving
resources and coding a little more prettily is a Good Thing (TM) IMO.



你可以在没有布尔的情况下编码。至于节省资源

是什么让你认为你在节省资源?如果您的程序

只使用一个bool,那么它仍然会存储在一个内存单元中

或一个寄存器中,这样你仍然会浪费比特。如果你在同一个程序中使用几个bools

,那么编译器可以将它们放在

相同的内存单元或寄存器中,然后检索每个的值

一个会慢一点,所以你会节省内存但会失去速度。

You can code just fine without a bool. As for saving resources
what makes you think that you''re saving any ? If your programme
uses just one bool then it will still be stored in one memory cell
or one register so you still get wasted bits. If you use several bools
in the same programme then the compiler could put them in the
same memory cell or register but then retrieving the value of each
one would be slower so you would save memory but lose speed.


所以......有人可以一劳永逸地向我解释这一点吗?我是

确定必须有一个合乎逻辑的解释,似乎没有人真正了解b
。疯狂必须结束。


bool some_bool = 0; / *它有多棒...... * /
So... can somebody properly explain this to me once and for all? I''m
sure there MUST be a logical explanation that nobody seems to really
understand. The madness must end.

bool some_bool = 0; /* How great it would be... */


这篇关于C中缺少布尔数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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