Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between _Bool and bool types in C?

Tags:

c

types

boolean

Can anyone explain me what is the difference between the _Bool and bool data type in C?

For example:

 _Bool x = 1;
  bool y = true;

  printf("%d", x);
  printf("%d", y);
like image 678
pr1m3x Avatar asked Jan 04 '12 09:01

pr1m3x


People also ask

What does _bool do in C?

_bool is a keyword in C Programming language representing boolean data type. It is an alternative to bool in C. In fact, bool is an alias to _bool. This was done considering the historic usage of C as an attempt to maintain compatibility.

Does bool type exist in C?

C does not have boolean data types, and normally uses integers for boolean testing. Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true.

When did bool add C?

An introduction to how to use booleans in C C99, the version of C released in 1999/2000, introduced a boolean type. To use it, however, you need to import a header file, so I'm not sure we can technically call it “native”. Anyway, we do have a bool type.


2 Answers

These data types were added in C99. Since bool wasn't reserved prior to C99, they use the _Bool keyword (which was reserved).

bool is an alias for _Bool if you include stdbool.h. Basically, including the stdbool.h header is an indication that your code is OK with the identifier bool being 'reserved', i.e. that your code won't use it for its own purposes (similarly for the identifiers true and false).

like image 63
Michael Burr Avatar answered Oct 18 '22 21:10

Michael Burr


There is no difference.

bool is a macro that expands to _Bool in stdbool.h.

And true is a macro that expands to 1 in stdbool.h

like image 22
ouah Avatar answered Oct 18 '22 19:10

ouah