KimmoA said:
That it makes sense to have a boolean type? I still want to know what
you all use instead...
Obviously I realize that nothing can be done about it now, but I want
to fully understand why they decided to design the language this way.
So far, nobody has really told me anything that truly convinces me that
it made sense.
It's because Dennis Ritchie wanted it that way.
At the time, it simply wasn't felt that it was necessary. C has the
convention that any scalar expression can be used as a condition (in
an if or while statement, or as an operand of "!", "||", or "&&"). If
the expression has the value zero, it's treated as false; if it has
any non-zero value, it's treated as true.
Given these conventions, a separate Boolean type simply is not needed.
In fact, C programmers have been writing code for decades without the
need for such a type. C is intended to be a relatively small
language. If you want a variable to hold a condition, just use an
int. For example:
int done = 0;
while (!done) {
...
if (...) {
done = 1;
}
...
}
You have to be careful about some things. For example this:
cond = isdigit(c);
if (cond == 1) {
...
}
is incorrect, because isdigit doesn't necessary return 0 or 1. But this:
cond = isidigit(c);
if (cond) {
...
}
is both correct and better stylistically.
As for storage size concerns, that's already been explained several
times. Storing a single Boolean object in a single bit is not
helpful; the rest of the byte or word containing that bit isn't going
to be used for anything anyway, the code to extract that single bit is
probably larger than the code to load a byte or word, and you wouldn't
be able to take the object's address. If you want large arrays of
booleans, large enough that storage size becomes a concern, you may
want to trade off code complexity against data size and use a packed
bit array. C has no syntax for doing this directly; apparently
there's never been enough of a demand for it to justify adding it to
the language. But again, this is something that can already be
implemented in the language itself.
Of course that's not the whole story. A *lot* of programmers have
implemented their own Boolean types, by various names and with various
definitions. See section 9 of the comp.lang.c FAQ,
<
http://www.c-faq.com/>, for some examples. The problem with that is
that types defined by different programmers may not be compatible, and
when their code is combined into a single program, it has to be
reconciled somehow. C99 added _Bool and <stdbool.h> to bring some
order to the situation *without* breaking any existing solutions.
None of this implies that C *couldn't* have had a built-in Boolean
type from the beginning. It easily could have. Maybe it even would
have been better if it had. C, like any language, was designed by
imperfect human beings (mostly by one particular human being). If you
don't agree with the decisions, that's fine, but there were valid
reasons.