Difference between _Bool and bool types in C?

CTypesBoolean

C Problem Overview


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);

C Solutions


Solution 1 - C

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).

Solution 2 - C

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionpr1m3xView Question on Stackoverflow
Solution 1 - CMichael BurrView Answer on Stackoverflow
Solution 2 - CouahView Answer on Stackoverflow