14

Any time I had the need of a Boolean type I was told to either create one, or better yet, use stdbool.h.

Since stdbool.h uses typedef bool _Bool, is there a reason to use the header instead just using type _Bool? Is it just for the additional macros (/* #define true 1 #define false 0 */)?

Giovanni Cerretani
  • 1,693
  • 1
  • 16
  • 30
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124

3 Answers3

16

The obvious type to add into the language was bool. But unfortunately, plenty of code was written that included bool in other shapes and forms. Recall that support for a boolean type was added only in C99.

So the C language committee had no choice but to pull out a reserved identifier for it (_Bool). But, since the obvious choice of type name is still the same, stdbool.h was added to allow users the obvious name. That way, if your code didn't have a home-brewed bool, you could use the built in one.

So do indeed use stdbool.h if you aren't bound to some existing home-brewed bool. It will be the standard type, with all the benefits that type brings in.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • Answer which actually gives the true reason: backward compatibility with already written code. Now: what are the examples of `other shapes and forms` of `bool` that were used in plenty of code? – pmor Jan 15 '21 at 13:27
  • @pmor - I worked at a shop that had its own basic const header that contained `typedef int bool;` - and it was all over the (entirely) C90 code base. Basically, many places would do one of these https://stackoverflow.com/questions/1921539/using-boolean-values-in-c – StoryTeller - Unslander Monica Jan 15 '21 at 13:34
  • Thanks for the example. Surely, it was asked before, but: why not standardizing `bool` type from the beginning (C89)? (So people had to redefine it over and over.) – pmor Jan 15 '21 at 21:53
  • @pmor - C was in widespread use for over a decade before being standardized. The first standard was likely meant to be a common baseline that folks could adopt. It couldn't have been added then either. – StoryTeller - Unslander Monica Jan 16 '21 at 11:51
4

The common practice has always been to use bool but when the type was officially introduced into the standard in C99, they didn't want to break the "roll-your-own" implementations. So they made the type _Bool as kind of a hack around the unofficial bools. Now there's no type name collision. Anyway, point is, use bool unless a legacy codebase breaks.

torstenvl
  • 779
  • 7
  • 16
4

They are same. bool is an alias for _Bool. Before C99 we used we dont have this type. (Earlier the use was limited to an integer tyoe with 0 as false and 1 as true).

You may not use it. Even you can undef bool (but it is recommended not to do so). But including it (stdbool.h and bool alias of _Bool) is good because then if someday it becomes reserved your code complies to that.1

1. You can use bool other way but it is better not to. Because in general when this stdbool.h is introduced it bears the plan of gradually making it standard and then even more stricter rule applies where we can't use bool as something other and it will be reserved as keyword.

user2736738
  • 30,591
  • 5
  • 42
  • 56
  • I know they are the same... that is why I asked. I'll edit my question to clarify – CIsForCookies Nov 19 '17 at 06:54
  • @CIsForCookies.: I just tried to say that you can use bool otherway but it is better not to. Because in general when this `stdbool.h`is introduced it bears the plan of gradually making it standard and then even more stricter rule applies where we can't use `bool` as something other and it will be reserved as keyword. – user2736738 Nov 19 '17 at 07:03
  • 1
    If you used `_Bool` in C90 code, you were cheating because the name was reserved for use by the implementation. Before C99, the standard C had no Boolean type; the nearest approximation was an integer type with zero as false and any non-zero value as true. – Jonathan Leffler Nov 19 '17 at 09:40
  • @JonathanLeffler.: ok I see..so before _Bool was not introduced it was introduced much later. But I saw code which used it..maybe it is used against the pratice? I edited now. – user2736738 Nov 19 '17 at 09:52