Re: "a < b < c" not the same as "(a < b) && (b < c)"?



Keith Thompson <kst-u@xxxxxxx> writes:

> Tim Rentsch <txr@xxxxxxxxxxxxxxxxxxx> writes:
> > August Karlstrom <fusionfive@xxxxxxxxx> writes:
> >> Keith Thompson wrote:
> >> > August Karlstrom <fusionfive@xxxxxxxxx> writes:
> >> >>OK, let me rephrase that: C has no native (primitive) boolean type.
> >> >
> >> > Yes, it does. It's called _Bool (or bool with "#include <stdbool.h>").
> >>
> >> OK, you're right, sorry. The existence of the header file has made me
> >> think it's not a native type. So _Bool is just an integer type that can
> >> hold two values: 0 or 1, and it's called _Bool just to minimize the risk
> >> of breaking existing programs, right?
> >
> > The type _Bool is an integer type that can hold only two
> > values, those being 0 and 1. However, it's different
> > from other integer types in some important ways:
> >
> > 1. Assigning a zero value to a _Bool results in 0, and
> > assigning any non-zero value to a _Bool results in 1.
> > (Same for conversion.) No other integer type behaves
> > this way (in all implementations).
>
> It's all about conversion. The only way to assign a non-zero value to
> a _Bool is to convert it to _Bool (unless it's already of type _Bool).
> The conversion rules say that the result is always 0 or 1.

Yes, but it's not only that the result is always 0 or 1.
An unsigned int bitfield of length one always holds either
0 or 1. But only _Bool maps any non-zero value to 1 and
only zero values to 0. There are conversions going on
in both cases (of bitfield and _Bool), but the conversion
rules for _Bool are different from those of any other
integer type.
.



Relevant Pages

  • Re: "a < b < c" not the same as "(a < b) && (b < c)"?
    ... So _Bool is just an integer type that can ... It's all about conversion. ... It's always legal to assign or convert a pointer ...
    (comp.lang.c)
  • Re: "a < b < c" not the same as "(a < b) && (b < c)"?
    ... >>> of breaking existing programs, ... >> The type _Bool is an integer type that can hold only two ... >> (Same for conversion.) No other integer type behaves ...
    (comp.lang.c)
  • Re: "a < b < c" not the same as "(a < b) && (b < c)"?
    ... >>> (Same for conversion.) No other integer type behaves ... > An unsigned int bitfield of length one always holds either ... > in both cases (of bitfield and _Bool), ...
    (comp.lang.c)
  • Re: conversions
    ... than or equal to the rank of int and unsigned int. ... supported by an implementation that have lower conversion rank than int. ... "The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, ...
    (comp.lang.c)
  • Re: conversions
    ... "Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank." ... "Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type." ...
    (comp.lang.c)

Loading