tags:

views:

662

answers:

5

I want to find the _Bool definition on my system, so for systems where it's missing I can implement it. I've seen various definitions for it here and on other sites, but wanted to check on the system for the definitive definition.

Slight problem, in that I can't find where _Bool is defined or even stdbool.h

mussys@debmus:~$ find /usr/include/* -name stdbool.h
/usr/include/c++/4.3/tr1/stdbool.h

And grep for _Bool on /usr/include/* and /usr/include/*/* does not find it either.

So where is it?

+2  A: 

_Bool is a built-in type, so don't expect to find a definition for it in a header file, even a system header file.

Having said that, guessing your system from the paths that you are searching, have you looked in /usr/lib/gcc/*/*/include ?

My "real" stdbool.h lives there. As expected it #defines bool to be _Bool. As _Bool is a type native to the compiler there's no definition for it in the header file.

Charles Bailey
Thanks. No hadn't checked there, but my system is the same yes.
James Morris
A: 
$ echo '_Bool a;' | gcc -c -x c -
$ echo $?
0

$ echo 'bool a;' | gcc -x c -c -
<stdin>:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘a’

This demonstrates that _Bool is a built-in type and bool is not, by compiling a single variable declaration with no includes.

Matt Joiner
`$ echo '_Bool a;' | gcc -std=c99 -x c -c -` ...`$ echo $?` ...`0` ... I see.
James Morris
maybe now it will make sense, i explained a bit.
Matt Joiner
+2  A: 

As a note:

The _Bool is defined in C99. If you build your program with:

gcc -std=c99

You can expect it to be there.

pbos
Thanks, but this is not what I was asking.
James Morris
I'd think that no such functionality definable in a c header. All types that exist wrap, and if you'd compare a (unsigned char) bool (= 2) with another bool(=3), they wouldn't match. So true != true, and that'd suck. Also bool b1 == true might also fail. As no datatypes (except \Bool) have this behaviour, you probably can't make one yourself, since it requires compiler backend support, and you cannot overload operators. You could do cmp_bool(bool b, bool b2), but that's probably *really* not what you wanted. I can't guarantee that I'm right, but I hope that added something atleast. :)
pbos
That's "underscore Bool", formatting got fscked up, and backslash underscore didn't help.
pbos
I see what you're saying, but why would I set a bool to 2 or 3? See bottom of the page I found while googling "enum bool c" http://www.lysator.liu.se/c/c-faq/c-8.html
James Morris
This would happen if you wanted to add boolean values (could be used, not required in any way). But if you wanted to typecast an int to a bool this could be an issue. (bool b = (bool)int_value;) instead of (bool b = int_value != 0;) So long as you're aware of the limitations and usage, it's not an issue. :)
pbos
James Morris
The Standard (section 6.3.1.2) says: "1 When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1.".
pmg
And what we're discussing here is how to implement bool when _Bool is missing.
pbos
+1  A: 

_Bool is a predefined type in C99, much like int or double. You will not find the definition for int in any header file either.

What you can do is

  • check the compiler is C99
  • if it is use _Bool
  • otherwise use some other type (int or unsigned char)

For example:

#if defined __STDC__ && defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
/* have a C99 compiler */
typedef _Bool boolean;
#else
/* do not have a C99 compiler */
typedef unsigned char boolean;
#endif
pmg
I'm using autotools which tells me about `_Bool` via `HAVE__BOOL` and `HAVE_STD_BOOL_H` but this way looks like a fair alternative.
James Morris
A shorter version would be "#ifndef _Bool", is there any non-standard way about writing that? I know that it works in gcc.
pbos
I did not expect `#ifndef _Bool` to work, and nor does it with the gcc (4.3.2 debian) I'm using.
James Morris
For binary compatibility reasons this might not be as good an idea as it looks. In general, when creating a public C API, it is best to simply use "int" wherever a boolean type is required.
Michael Aaron Safyan
@Michael: I mostly use C89 and I tend to use `int` when I need a boolean. I checked `sizeof (_Bool)` before posting and, as it is 1 that's why I opted for `unsigned char` in my answer
pmg
+1  A: 

other people have well-replied to the question on _Bool location and finding if C99 is declared... however, i am not satisfied with the self-made declaration everyone gave.

why won't you completely define the type ?

typedef enum
{
    false = 0,
    true  = 1,
} bool;
Adrien Plisson
I think it's overkill after I decided to google "bool enum c" which sent me to http://www.lysator.liu.se/c/c-faq/c-8.html - see bottom of page regarding the danger of #defining true to be 1·
James Morris
i do agree with the arguments provided in your link. personally, i define true and false for clarity when assigning to a boolean variable. coming from a strongly-typed background, for me, a bool is not a char nor an int, it is a type by itself, thus the enum definition above. also, i do think that automatic conversion from int to bool is not quite a good idea (and this applies to any automatic conversion).
Adrien Plisson
What if the header is included from a C++ source file? Better to not use the keywords "bool", "true", and "false" when defining the type.
Michael Aaron Safyan
we are talking about C: C does not define a lot of concepts that we have to define ourself. the problem would arise with any identifier which match a C++ keyword: static_cast, this, ...
Adrien Plisson