473,804 Members | 4,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Absense of 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.

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... */

Nov 3 '07 #1
51 2930

"AommiK" <no****@nospam. comwrote in message
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.
C is portable assembly. Generally there is no way of manipulating single
bits with assembly instructions, though of course you can do so with a
combination of instructions.
So bool wasn't included.
We've moved on a little since, and there is quite a good case for it, since
it doucments that a variable is flag, and that is frequently what we want.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Nov 3 '07 #2
AommiK wrote:
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.
At best this is a minor irritation, not a major flaw IMHO.
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.
It was not a badly felt need during much of C's evolution and still is
not.
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.
Use bitfields.
"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.
Because bool is too often an overrated type. In anycase with most
machines bool is simulated by either a byte of a packed type.

If you're bothered about space why not use bitfields?
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.
This can be done. It wasn't important enough to be Standardised.
bool some_bool = 0; /* How great it would be... */
You can do this since C99.

Nov 4 '07 #3
On Nov 3, 10:56 pm, AommiK <nos...@nospam. comwrote:
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.
Why is that a MAJOR flaw? I have trouble seeing it as even a minor
flaw.
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.
Why?
Char is a small int. We all know that. However, "char some_bool = 0;"
simply feels wrong,
Why?
and I think that most of you agree.
I doubt that very much.
Plus, it's still too large.
It's typically the smallest addressable type. If you want smaller
(with the added overhead of more code to implement it) use a bit
field.
"int some_bool = 0;" is what I -- and everyone else, I assume -- use
for bools.
Why do you assume that?
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?
That's a very silly question. They didn't see a need for it because
they didn't see a need for it. You need to explain why you think they
should have seen a need for it.
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.
A value of zero is false, non-zero is true. Why do you need a
particular type to hold the value?
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?
You're not necessarily silly for bringing it up, but you're doing it
in a silly way. You need to explain why you think it was needed, and
why anyone should have seen a need for it. You seem to assume that
everyone shares your view of C and its history and agrees with you for
some reason.
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.
How would it save resources? A boolean type smaller than char would
require more resources to implement and at run time. How would it make
coding any prettier?
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.
I've no idea what you're talking about.
bool some_bool = 0; /* How great it would be... */
It's there in C99, pretty pointlessly I think. What is the advantage
of this type? What does it buy me over char or int?

Nov 4 '07 #4
AommiK <no****@nospam. comwrites:
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.
[...]

On typical systems, allocating 8 or more bits to an object intended
only to hold the values 0 and actually saves space. Accessing a
single bit, without disturbing the rest of the byte or word that it's
a part of, is more expensive than accessing the entire byte or word.
Most CPUs can't directly address single bits.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 4 '07 #5
AommiK wrote:
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.
It's probably this "MAJOR" flaw that has kept C on the
sidelines and prevented it from gaining any noticeable following.
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.
If you'll spend several dozens of hours and many thousands
of dollars to lie on a couch and chat with a professional about
your dreams and your early childhood memories, your obsession
may turn out to be curable.
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 sympathize. For me, the thing that makes no sense whatsoever
is quantum entanglement. However, the fact that it makes no sense
to me doesn't impel me to the belief that QE is a "MAJOR" flaw in
Nature. You seem to attach more importance to your own befuddlement
than I do to mine.
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.
Kidding aside, there's the crux: You declare that you do not
need a Boolean type "to get anything done."

Say your words over again, slowly: You claim you

DO

NOT

NEED

a Boolean type. Why, then, is C deficient in a "MAJOR" way for
lacking what you DO. NOT. NEED? C also lacks quaternions, nested
functions, three-way ifs, and the kitchen sink -- "MAJOR" omissions
all, I guess you'd say, whether you would use them or not.
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.
The "madness," if you choose to think of it that way, ended
eight years ago. Haven't you grown weary of tearing your hair
and rending your clothing? Aren't the sackcloth and ashes
getting a little tiresome? Your stamina is admirable, but ...
bool some_bool = 0; /* How great it would be... */
Go ahead. No one is stopping you, except possibly your peers
in code review who will (1) object to your idiosyncratic way of
spelling `false' and (2) ask what the purpose of `some_bool' is.
In my experience, programs in all programming languages use Boolean
*values* all the time, but the use of Boolean *variables* is far
rarer.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Nov 4 '07 #6
AommiK wrote:
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.
Do you have an example of a language with a boolean type which will
store the boolean in a location smaller than a byte?

(C optimizers are allowed to reduce the size used by bool types; but I
would guess that the performance hit by requiring all those extra
bit-twiddling operations would generally be considered to much.)

I agree that it's useful to have a bool type for code readability
purposes; but you have always been able to typedef one. However I'm just
not convinced that a bool should be stored by default in a single bit,
within a byte shared with other bool values.

--
Philip Potter pgp <atdoc.ic.ac. uk
Nov 4 '07 #7
On Sun, 04 Nov 2007 09:53:31 +0000, Philip Potter wrote:
(C optimizers are allowed to reduce the size used by bool types; but I
would guess that the performance hit by requiring all those extra
bit-twiddling operations would generally be considered to much.)
They can't. See 6.2.6.1p2.
Also, I must be allowed to access them as arrays of unsigned char,
if several _Bool are stored in the same byte
memset(&bool_va r, 0, sizeof bool_var) could affect other ones,
which 6.2.4p2 forbids. Also, if the bytes of an object are
themselves objects, if I do *(unsigned char *)&bool_var = '/';
that byte will stay the same until I access bool_var again (it
could change even if I just *read* it, because '/' could be a
trap representation for _Bool causing UB, but if I don't access it
at all, it can't change).
As I see it, _Bool is required to have at least seven padding
bits. (Anyway, I think that if a _Bool is declared as register, or
if the compiler pretends that it is because its address is never
taken, it might not actually store them anywhere.)
--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.
Nov 4 '07 #8
On Sat, 03 Nov 2007 19:05:56 -0700, J. J. Farrell wrote:
It's there in C99, pretty pointlessly I think. What is the advantage
of this type? What does it buy me over char or int?
The fact that the implementation can choose which one it is.
(So can it with enum { false, true }, but the latter in some
implementations turns out to be much larger.)
Also, (char)0.1 and (int)I are zero, whereas (_Bool)0.1 and
(_Bool)I are one.
--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

Nov 4 '07 #9
On Sat, 03 Nov 2007 19:05:56 -0700, J. J. Farrell wrote:
It's there in C99, pretty pointlessly I think. What is the advantage
of this type? What does it buy me over char or int?
The fact that the implementation can choose which one it is.
(So can it with enum { false, true }, but the latter in some
implementations turns out to be much larger.)
Also, (char)0.1 and (int)I are zero, whereas (_Bool)0.1 and
(_Bool)I are one. (But one could simply write
char foo = (bar != 0); so that doesn't buy much...)
--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

Nov 4 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2613
by: Pierre Espenan | last post by:
A have a long integer class. The built integer type within a conditional statement returns bool false for int i=0 and bool true for any other non zero value. I want my long integer class to have similar behavior. My class looks like this: #ifndef long_int_H #define long_int_H #include <string> using namespace std; typedef valarray<complex<double> > VCD;
19
3365
by: daniel | last post by:
1) is C++ smart enough to automatically use "bits" for bool or will a bool have the size of a charcter (byte). 2) The index of a vector is it an integer (4 byte) or a "long long" with 8 bytes or something else? I ask because I want to use vector<bool> with a few billion entries exceeding the int32 range for indexing and I want to use as few memory as possible for the whole
4
10756
by: ORC | last post by:
Is the bool type actually an Int32 with 0 as false and non zero as true? The reason for my question is that I've seen a lot of API calls that return a Int32 implemented in C# as an bool like: private static extern bool PurgeComm( Int32 hFile, UInt32 dwFlags); Thanks Ole
6
6831
by: zl2k | last post by:
hi, there I am using a big, sparse binary array (size of 256^3). The size may be changed in run time. I first thought about using the bitset but found its size is unchangeable. If I use the vector<bool>, does each element takes 4 bytes instead of 1 bit? I am using gcc3.4.4. There is a bit_vector which is kind of old so I wont use that. Any other choices? Thanks ahead. zl2k
64
3904
by: shaanxxx | last post by:
I have code which says #define MYBOOL int This code is very old. people who have written is not avaible. I was thinking what could be reason. 1) bool datatype was not available that time (10 years back: correct me if i am wrong) 2) int was word aligned (16bit and 32bit). Writter of above code doesnt want to give choice to compiler.
3
6327
by: markb | last post by:
Hi My C# app is being called from a callback from an unmanaged DLL. One of the parameters of the callback is of type BOOL. I am using PInvoke to marshal this to a (managed) bool. The problem is that no matter if we pass TRUE or FALSE, the bool is always marshalled as true. // unmanaged code in dll typedef bool (__stdcall *BoolCallBack)(short b);
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10577
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10320
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9150
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.