Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is C standard bool not bool_t?

Tags:

c

types

boolean

C's stdbool.h adds a #define for the type _Bool to bool.

I know a #define was used instead of a typedef to allow #undef-ing for legacy code clashes. But why wasn't bool_t (to match the other standard types) used in place of bool? Is the _t used for typedef types only?

I ask because the platform I am working on adds a typedef for bool_t to _Bool.

EDIT: It's been correctly pointed out that the following is probably opinion based so consider it as only my reason for the primary question above.

So I'm wondering should I use that or continue to use bool? I kind of like the way the former makes the boolean standard type form match the other standard types.

like image 702
Toby Avatar asked Sep 01 '25 05:09

Toby


2 Answers

C introduced the bool macro (that expands to _Bool) with the C99 Standard. They probably chose the name bool instead of bool_t because C++ in 1998 named their boolean type bool. In C++98 all integral types (except wchar_t already in C90) don't use a _t suffix.

like image 97
ouah Avatar answered Sep 02 '25 19:09

ouah


I think you just found that the standard is inconsistent. bool is not the only type whose name does not have the _t suffix. Consider, for example, <setjmp.h>. It defines jmp_buf which is clearly a typedef yet does not end in _t.

I think the main reason why it's not bool_t is to make it look like it belongs to the family of standard types, int etc. However, they couldn't name the type bool due to compatibility purposes so hence the _Bool.

As for whether you should use bool or bool_t I think the question is rather opinion-based and cannot be answered just by using facts. However, I recommend you select one or the other and document that decision well in your coding guidelines.

like image 40
juhist Avatar answered Sep 02 '25 20:09

juhist