sramij
sramij

Reputation: 4925

Calling C function taking bool, not BOOL, from ObjC

Within my ObjC code, I am calling a function which has an argument of type bool (not BOOL).

Should I be passing YES to this argument from my ObjC code, or would passing true should be correct?

This question is about conventions, not functional correctness.

Upvotes: 1

Views: 51

Answers (2)

CRD
CRD

Reputation: 53000

The convention, if one exists in any formal sense, would be for bool/true/false and BOOL/YES/NO.

As for functional correctness you are correct that it is hard (but not impossible[1]) to "contrive an example which demonstrates a difference". Boolean logic in C is based on integers, "boolean" valued operators (>, &&, et al) return integers not any flavour of boolean.

HTH


[1] E.g. consider uses of @encode and different platforms (macOS, iOS, etc.) if you wish to contrive something.

Upvotes: 2

Sulthan
Sulthan

Reputation: 130102

This really depends on your build settings. See the related answer Objective-C : BOOL vs bool

BOOL and bool can refer to the same type but sometimes they don't.

To be safe, always use the correct type, that is, false and true for bool.

The difference can be seen mostly in rare edge cases but if it appears, it's really difficult to find.

For the same reason we use NULL instead of nil when interacting with C APIs, even if they both are usually just a macro for 0. Their types still differ.

Upvotes: 2

Related Questions