0

As for stdbool.h, I can see some people wanting to have constants for true and false and a type named bool if only for clarity (though I'm not really one of them, personally).

However, what's the purpose of the actual _Bool type? Why not just define the bool type to be int (when that's what actual boolean expressions evaluate to anyway)? Or char if size is a concern.

Does it license the compiler to do things it couldn't do with ints? Does it provide for some kind of type-safety?

Dolda2000
  • 25,216
  • 4
  • 51
  • 92
  • @alk: How did I just post an answer when you'd already closed the question?! – user541686 May 24 '14 at 08:16
  • @alk: Thanks. I thought I searched properly for the question, but I seem to have failed to find it anyway. – Dolda2000 May 24 '14 at 08:16
  • @Mehrdad: You seem to have beat me by at least a second. – alk May 24 '14 at 08:17
  • @alk: On the other hand, it seems to me that the answers to that question don't match my question at all. I can't figure out why the accepted answer was accepted, since it doesn't mention the utility of `_Bool`, merely that if `bool`. – Dolda2000 May 24 '14 at 08:23
  • From the linked question's accepted answer: "*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).*" – alk May 24 '14 at 08:24
  • @alk: I read "the same facilities" as C++ as being the `bool`, `true` and `false` names, which is not what I was asking about. Am I misunderstanding the answer, and those "facilities" are in fact something else? – Dolda2000 May 24 '14 at 08:25
  • You indeed could "emulate" a real boolean by using any integer, that's true. And if that's ok for you , you do not need to use a real boolean. A "real boolean" could only carry two values that is: "true" or "false". – alk May 24 '14 at 08:28
  • @alk: Should I interpret that as meaning that a `_Bool` variable, when read back, can only yield the values 0 or 1? In that case, that would be the answer to my question, I guess. Unfortunately, it's not mentioned in the answers to the linked question. ;) – Dolda2000 May 24 '14 at 08:30
  • @alk: Are you sure? You closed the question at 15m 20s past the hour, my answer is dated 15m 39s past the hour... that seems to imply you beat me by 19s. – user541686 May 24 '14 at 08:31
  • @Mehrdad: This looks like just another race ... Btw: where do you get those exact times from? – alk May 24 '14 at 08:32
  • 1
    @alk: Sorry, I guess it was actually "mentioned", looking at the answer again; it would be more accurate to say that it wasn't made the main point of the answer (which focused more on the parallels to C++), but more like mentioned in passing. – Dolda2000 May 24 '14 at 08:34
  • @Dolda2000 I just added an the answer in the duplicate that explains a difference between `_Bool` and the other integer types http://stackoverflow.com/a/23842997/1119701 – ouah May 24 '14 at 08:39

1 Answers1

0

I think (but I'm not sure) the reason is that many people already had bool defined in their projects, and defining bool as a primitive type would have broken such code.

user541686
  • 205,094
  • 128
  • 528
  • 886
  • Thanks, but that's not what I was asking. I wasn't wondering why `bool` wasn't defined as a primitive type, but rather why there was felt a need for a new primitive type at all. – Dolda2000 May 24 '14 at 08:21
  • @Dolda2000: Are you asking why `bool` is useful? – user541686 May 24 '14 at 08:32
  • No, I'm asking why `_Bool` is useful, as opposed to the pre-existing numeric datatypes, but I already got my answer in the comments, by alk. – Dolda2000 May 24 '14 at 08:39