David542
David542

Reputation: 110163

What advantage does _Bool give?

If the _Bool type acts like an integer and doesn't enforce that a value is true/false or 1/0, for example:

_Bool bools[] = {0,3,'c',0x17};
printf("%d", bools[2]);

> 1

What is the advantage of having that there? Is it just a simple way to coerce things to see how they would evaluate for 'truth-ness', for example:

printf("%d\n", (_Bool) 3);
> 1

Or how is this helpful or useful in the C language?

Upvotes: 4

Views: 764

Answers (3)

chux
chux

Reputation: 153457

What advantage does _Bool give?

  1. The value of a _Bool is either 0 or 1. Nothing else, unlike an int.

  2. Conversion to a _Bool always converts non-zero to 1 and only 0 to 0.

When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1.

Examples:

#include <math.h>
#include <stdlib.h>
_Bool all_false[] = { 0, 0.0, -0.0, NULL };
_Bool all_true[] = { 13, 0.1, 42.0, "Hello", NAN };

Notice the difference of conversion/casting to int vs; _Bool: (int) 0.1 --> 0, yet (_Bool) 0.1 --> 1.

Notice the difference of conversion/casting to unsigned vs; _Bool: (unsigned) 0x100000000 --> 0, yet (_Bool) 0x100000000 --> 1.

  1. _Bool adds clarity to boolean operations.

  2. _Bool is a distinctive type from int, char, etc. when used with _Generic.

  3. Prior to C99, C lacked _Bool. Much early code formed their own types bool, Bool, boolean, bool8, bool_t, .... Creating a new type _Bool brought uniformity to this common, yet non-uniform practice. <stdbool.h> is available to use bool, true, false. This allows older code, which does not include <stdbool.h> to not break, yet newer code to use cleaner names.


OP's example with "doesn't enforce that a value is true/false or 1/0" does enforce that bools[2] had a value of 1. It did not enforce that the initializer of 'c', an int, had to be in the range of [0...1] nor of type _Bool, much like int x = 12.345; is allowed. In both cases, a conversion occurred. Although the 2nd often generates a warning.

Upvotes: 6

Consider this:

(bool) 0.5 -> 1
( int) 0.5 -> 0

As you can see, _Bool does not act like an integer.

Upvotes: 1

tadman
tadman

Reputation: 211580

The advantage is legibility, nothing more. For example:

bool rb() {
  if (cond && f(y)) {
    return true;
  }

  return false;
}

Versus:

int rb() {
  if (cond && f(y)) {
    return 1;
  }

  return 0;
}

There's really no other benefit to it. For those that are used to working in C code without bool, it's largely cosmetic, but for those used to C++ and its bool it may make coding feel more consistent.

As always, an easy way to "cast to a boolean value" is just double negation, like:

!!3

Where that will reduce it to a 0 or 1 value.

Upvotes: 2

Related Questions