Difference between _Bool and bool types in C?

57,660

Solution 1

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

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

Share:
57,660
pr1m3x
Author by

pr1m3x

Updated on August 01, 2020

Comments

  • pr1m3x
    pr1m3x almost 4 years

    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);
    
  • tialaramex
    tialaramex almost 12 years
    The long term intention seems to be that the standard will eventually be revised to make bool a keyword. This is step 1, where you can use <stdbool.h> to get the bool macro and you're still permitted to undefine or redefine it for your own nefarious purposes. Step 2 will be to refuse to undefine or redefine bool from <stdbool.h> to discourage such shenanigans. Then step 3 is to make bool a keyword exactly like _Bool and mark _Bool deprecated. At the end of this long road (say, a decade or two from now) standard C code will have a bool type, and new programmers won't know it ever lacked one.
  • Ricardo Sanchez-Saez
    Ricardo Sanchez-Saez almost 11 years
    I don't understand why this has to take two decades given that it is quite an obvious improvement. Code that does not like this change can still be compiled using current compiler versions.
  • zwol
    zwol about 9 years
    I do not think the C standard will ever be revised to make bool a keyword. Consider that the type of string literals is still char *, and you can still use unprototyped function declarations.
  • S.S. Anne
    S.S. Anne over 4 years
    @zwol You can't use unprototyped function declarations since after C90.
  • zwol
    zwol over 4 years
    @JL2210 The language accepted by default, by all of the compilers with significant mindshare, includes unprototyped function declarations, and I don't expect that ever to change.
  • S.S. Anne
    S.S. Anne over 4 years
    @zwol Yes, but that's not the C standard.
  • zwol
    zwol over 4 years
    @JL2210 Yes, but everyone who isn't a language lawyer thinks of the language-accepted-by-default (i.e. C11 + whatever set of extensions is active by default) when they think of C11, therefore saying "you can't use unprototyped function declarations since after C90" is just going to confuse people even more than they already are.
  • zwol
    zwol over 4 years
    @JL2210 I'd put it like this: "Unprototyped function declarations are an obsolete feature of the original 1989 C standard. They were removed from later revisions of the standard, but are still accepted by modern compilers for backward compatibility's sake. Don't use them in new code."