views:

222

answers:

4
+2  Q: 

bool versus BOOL

Possible Duplicates:
Objective-C : BOOL vs bool
Is there any difference between BOOL and Boolean in Objective-C?

I noticed from the autocomplete in XCode that there is a bool and a BOOL in Objective-C. Are these different? Why are there two different kinds of bool?

Are they interchangeable?

+1  A: 

BOOL is actually a signed char (thanks Yuji), while bool is a true boolean from the ISO C99 standard.

See here: http://iphonedevelopertips.com/objective-c/of-bool-and-yes.html

Don
Strange, in Windows, `BOOL` is actually an `int`!
Blindy
In C++, `char`, `signed char` and `unsigned char` are all different. So, in Objective-C++, `BOOL` is not a `char`; it's a `signed char`.
Yuji
You're right - I'll edit my answer since I see it's grammatically incorrect anyway.
Don
+5  A: 

BOOL is defined in Objective-C as typedef signed char BOOL, while bool is the datatype defined in C99.

kiamlaluno
`bool` is also defined in C99 in stdbool.h.
Chuck
@Chuck: You are, indeed, correct. I have corrected my answer.
kiamlaluno
A: 

You should use bool unless you need to interoperate with code that uses BOOL, because bool is a real Boolean type and BOOL isn't. What do I mean "real Boolean type"? I mean that code like this does what you expect it to:

#define FLAG_A 0x00000001
#define FLAG_B 0x00000002
...
#define FLAG_F 0x00000020
struct S
{
    // ...
    unsigned int flags;
};

void doSomething(S* sList, bool withF)
{
    for (S* s = sList; s; s = s->next)
    {
        if ((bool)(s->flags & FLAG_F) != withF)
            continue;
        // actually do something
    }
}

because (bool)(s->flags & FLAG_F) can be relied upon to evaluate to either 0 or 1. If that were a BOOL instead of a bool in the cast, it wouldn't work, because withF evaluates to 0 or 1, and (BOOL)(s->flags & FLAG_F) evaluates to 0 or the numeric value of FLAG_F, which in this case is not 1.

This example is contrived, yeah, but real bugs of this type can and do happen all too often in old code that doesn't use the C99/C++ genuine boolean types.

Zack
For Objective-C, this is not good advice and is not the convention.
chpwn
If the convention is to use the more error-prone programming style, then the convention is wrong IMO.
Zack
chpwn: I don't see how conforming to the convention conflicts with the advice in the answer; he says “unless you need to interoperate with code that uses `BOOL`”, and all Cocoa code is such code. ☺ Moreover, if you read his demonstration, `bool` does look really handy.
Peter Hosey
dreamlax
Zack
+2  A: 

Yes they are different.

  • C++ has bool has a true boolean type. It is guaranteed to be 0 or 1 within integer context.
  • C99 has _Bool as a true boolean type, and if <stdbool.h> is included, then bool becomes a typedef for _Bool (this header also defines true and false).
  • Cocoa has BOOL as a type, but it is just a typedef for signed char.
  • Carbon has Boolean as a type, but it is just a typedef for unsigned char.
dreamlax