Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use <stdbool.h> instead of _Bool?

Tags:

c

boolean

stdbool

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 */)?

like image 328
CIsForCookies Avatar asked Nov 19 '17 06:11

CIsForCookies


Video Answer


1 Answers

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.

like image 141
StoryTeller - Unslander Monica Avatar answered Oct 21 '22 21:10

StoryTeller - Unslander Monica