Simple question really; is there a difference between these values (and is there a difference between BOOL and bool)? A co-worker mentioned that they evaluate to different things in Objective-C, but when I looked at the typedefs in their respective .h files, YES/TRUE/true were all defined as 1 and NO/FALSE/false were all defined as 0. Is there really any difference?
Simple question really; is there a difference between these values (and is there a
Share
There is no practical difference provided you use
BOOLvariables as booleans. C processes boolean expressions based on whether they evaluate to 0 or not 0. So:means the same as
which is why you can evaluate any primitive type or expression as a boolean test (including, e.g. pointers). Note that you should do the former, not the latter.
Note that there is a difference if you assign obtuse values to a so-called
BOOLvariable and test for specific values, so always use them as booleans and only assign them from their#definevalues.Importantly, never test booleans using a character comparison — it’s not only risky because
someVarcould be assigned a non-zero value which is not YES, but, in my opinion more importantly, it fails to express the intent correctly:In other words, use constructs as they are intended and documented to be used and you’ll spare yourself from a world of hurt in C.