Re: Whats the point of bool?



On Jun 3, 6:04 pm, chad <cdal...@xxxxxxxxx> wrote:
On Jun 3, 3:46 pm, Keith Thompson <ks...@xxxxxxx> wrote:



kid joe <spamt...@xxxxxxxxxxxxxxxx> writes:
I know that from a C point of view, the bool or __Bool type was introduced
into C to standardize common practise in existing compilers, but from a
historical perspective does anyone understand what was the motivation for
languages like C++ and Java (and eventually C) to have a seperate type for
boolean variables instead of just using ints?

It seems pretty illogical to me, since I dont believe theres any common
hardware where individual bits can easily be addressed, so whats the gain
from having a type thats essentially an alias for int at the hardware
level, versus the added complexity for compiler writers and programmers?

It's the same reason languages distinguish between integers and
floating-point numbers, for example.  On the hardware level they're
both machine words; we just apply different operations to them.  And
in fact one of C's ancestors (either B or BCPL, I don't remember
which) actually didn't make that distinction; it merely used different
operators for integer vs. floating-point addition and so forth.

It's for the benefit of the human reader.  Declaring an object as
bool, or int, or double, lets the reader know what it's to be used
for, and lets the compiler generate the right code for it (or reject
operations that don't make sense).

In some languages that are more strongly-typed than C, you can't even
perform an "and" or "or" operation on integers, or "+" or "-" on
booleans.  C is a lot looser, but it's still useful to be able to make
the distinction.

Okay, at the risk of going off topic, what strongly-typed languages
can't peform "and" or "or" operations on integers, or "+" or "-" on
booleans? And more to the point. Why is this?

You can't do it in Postscript, either.

--
lxt
.



Relevant Pages

  • Re: Fridays the thirteenth. (And a little puzzle.)
    ... -- compiler) is the usual method ... int febdays ... -- We're going to go round a loop dealing with each year in turn. ... -- other languages call) ...
    (uk.people.silversurfers)
  • Re: integer size and portability-a wishful controversy
    ... >> BTW, there is no point in using the old Turbo C compiler, unless ... I do NOT consider programs relying on int having at least 32 bits as ... use the PC as an almost freestanding platform for developing/testing ... device drivers for various pieces of PC hardware. ...
    (comp.lang.c)
  • Re: Error handling library
    ... which lets the compiler catch out-of-range usage. ... and assuming that a higher int means "more dangerous error" ... with a comment warning that one is used as an index into the array ... languages while running (an array of languages, ...
    (comp.lang.c)
  • Re: Whats the point of bool?
    ... hardware where individual bits can easily be addressed, ... versus the added complexity for compiler writers and programmers? ... It's the same reason languages distinguish between integers and ...
    (comp.lang.c)
  • Re: Code Review - is this code shit
    ... main (int) can be invoked. ... it using some constructs that aren't portable or compiler ... Even interpreted languages have this problem. ... Richard Heathfield constantly abuses and harasses unsuspecting users ...
    (comp.lang.c)

Loading