tags:

views:

156

answers:

1

I know BOOL is actually a typedef of signed char. But what about Boolean? I am confused of bool, Boolean and BOOL.... Q Q

+5  A: 

Boolean is an old Carbon keyword (historic Mac type), defined as an unsigned char. BOOL is an Objective-C type defined as signed char. bool is a defined version of the _Bool standard C type. It's defined as an int. Use BOOL.

jshier
`Boolean` is used in CoreFoundation (which Carbon is mostly built on top of now), so while you should use BOOL in objective-C, when dealing with any CoreFoundation APIs, Boolean is still in use.
bobDevil
Oh, I see, so I should use BOOL for common use and Boolean in CF. Thanks!
Frost
Oops, you're right. CoreFoundation does define a version of `Boolean`, but that's separate from Carbon's definition. See MacOSTypes.h vs. OSTypes.h. And on 64-bit systems that don't use C99, CoreFoundation's `Boolean` is an `unsigned char`.
jshier
From objc.h:`// BOOL is explicitly signed so @encode(BOOL) == "c" rather than "C" // even if -funsigned-char is used.` So BOOL for Objective-C, Boolean for everything else.
jshier
If `stdbool.h` is included in C99, then `bool` is a macro expanding to `_Bool`, which is a built-in true Boolean type representing either 0 or 1, it is not an `int`.
dreamlax
Nowadays, one should always use `bool` (including `stdbool.h` if not coding in [Objective] C++) except to interoperate with existing APIs that use one of the older types. Using a real Boolean type (can only take the values 0 and 1, any nonzero integer converts to `true` automagically) obliterates an entire class of possible bugs.
Zack