Re: rationale for #define true 1 in stdbool.h



Eric Sosman wrote:

> Personally, I still don't understand the motivation for
> adding _Bool to the language.

For me, it's desirable because assigning any non-zero value
to it causes it to have a non-zero value. This is not true for
any builtin type except for unsigned long long, which would
be a waste of memory if it were used as a boolean type.

I have accidentally written code like this:

bool b = (flags & FLAG_FOO);

where FLAG_FOO is something like 0x100. It took a
long debugging session to track down the problem; even
when I'd isolated the problem to this one block of code,
I still couldn't for the life of me figure out what was going on,
until I looked up the definition of 'bool'. (It turned out to
be a typedef for unsigned char).

.



Relevant Pages

  • [PATCH] drivers/block/DAC960: Converted boolean to bool
    ... Converts 'boolean' to 'bool' and removes the 'boolean' typedef. ... unsigned char CommandOpcode2, ... typedef struct DAC960_SCSI_RequestSense ...
    (Linux-Kernel)
  • Re: A way to bitwise AND two ints
    ... > As defined by Kernighan and Ritchie any non-zero is true. ... boolean type. ... you want positive ints to count as true. ... > int's together and get a bool" was a dead giveaway. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [RFC][PATCH] A generic boolean
    ... + if it is suppose to be an boolean, why not decleare it as one. ... -C++ and Java fans will treat bool as a green light to the following ... +typedef unsigned char bool; ... Why unsigned char? ...
    (Linux-Kernel)
  • Re: bool or BOOL in MFC projects
    ... which failed under Windows 95 because the API returned a non-zero ... In fact, correct me if I am wrong, expressions comparing integers a<b a==b return an integer 1 or 0 don't they? ... My 1991 copy of Stroustrup predates "bool", and so I am never sure about things like this :-) They haven't changed it have they? ... If not, the BOOL concept appears implicit in the language, even if it is Windows which actually defines the type BOOL. ...
    (microsoft.public.vc.mfc)
  • Re: [PATCH] Introduce a boolean "single_bit_set" function.
    ... +bool single_bit_set ... unsigned char *nrbitsp) ...
    (Linux-Kernel)