4

I am reading a book on C. It says that C99 added a data type _Bool. It is basically an int but stores only 0 or 1. Now I do not understand why there is a need of such a data type. We already have bool which implicitly translates to int and vice versa. So can somebody please tell me a situation where such a data type would be useful.

PS: C++ does not seem to support such a data type as seen here.

#include <iostream>
using namespace std;

int main() {
    // your code goes here
    _Bool b = false;
    if(b == 0)
        printf("FALSE");
    else
        printf("TRUE");
    return 0;
}
Cool_Coder
  • 4,888
  • 16
  • 57
  • 99

4 Answers4

9

_Bool in C doesn't have the same semantic than the other integer types.

For example, for the conversions to integers:

 (_Bool) 0.5

evaluates to 1

whereas

 (int)  0.5

evaluates to 0.

(This is the example given by the C99 Rationale for the _Bool type).

ouah
  • 142,963
  • 15
  • 272
  • 331
  • Do you know of a good source of information outlining the complete semantics? – Dolda2000 May 24 '14 at 08:41
  • @Dolda2000 I don't, the best is to look at the Standard directly. – ouah May 24 '14 at 08:44
  • @Dolda2000 Another thing with `_Bool` that you couldn't do with C89 basic integer types, is `(_Bool) integer_value` is a shorthand for `!!integer_value`. – ouah May 24 '14 at 08:45
  • @ouah: So you're saying that adding the new type made it possible to do in seven or nine characters something that could otherwise be done in two or four (depending upon the need to add parentheses around the following expression in either case)? – supercat Jun 21 '16 at 17:51
5

It says that C99 added a variable _Bool

No, C99 added a built-in type called _Bool, which can have values of either 0 or 1. The header, <stdbool.h> defines macros in which bool expands to _Bool, false to 0, and true to 1.

C++, on the other hand, has a built in type called bool, which can have values of true and false. For compatibility, C++11 specifies that stdbool.h should be present, but empty. (Some C compilers provided C++'s bool as an extension pre-C99.)

The intention with the C99 additions was to provide the same facilities as C++, but in a way that didn't invalidate old C89 code (where plain bool was available as a name). In my opinion the macro solution they came up with is less than ideal, and indeed it's still quite rare to see C code which uses the boolean types, whereas they are pervasive in C++.

Tristan Brindle
  • 16,281
  • 4
  • 39
  • 82
3

We already have bool

bool is a macro for _Bool. That's why we have _Bool. (Naming it bool would've broken code that already used the name; _Bool is less likely to collide with anything.)

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • 3
    User-defined names aren't allowed to start with an underscore followed by a capital letter, so `_Bool` was not just less likely to collide with anything, it was impossible (in standard compliant code). – Tristan Brindle Jan 29 '14 at 03:12
2

C++ does have a bool type, and so doesn't bother with _Bool. Before C99, C did not have a bool type; and from C99 on the <stdbool.h> header defined bool as a macro that expanded to _Bool.

tabstop
  • 1,751
  • 11
  • 10