Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ObjC BOOL and boolean c expressions



> I don't know about earlier versions, but the C99 spec
> guarantees that they will return 1 if true and 0 if
> not. The logic for determining whether something is
> true or false still boils down to "zero means false,
> nonzero means true," obviously, but the operators
> themselves can only return 1 or 0.

Yes, and I do believe that logic applies to C much earlier than C99. The
"gotcha" I think is that the actual bool type performs coercion such that it
only ever holds true or false (0 or 1). So:

 bool foo = 42;
 if( foo == true )

Takes the branch, exactly as:
 bool foo 42;
 if( foo )

Would. However Objective-C BOOL, as pointed out before, is just a typedef
for a char, and YES & NO are just typedefs, so there is no coercion, so:

 BOOL foo = 42;
 if( foo == YES )

Would not take the branch.

Of course there is no need to ever write "if( foo == YES )", so it's an easy
problem to avoid--just learn that a variable of type bool/BOOL is an
expression of type bool/BOOL and thus does not need to be compared to a
bool/BOOL value in order to yield a bool/BOOL result.

In other words, treat C like C, not like some >20 year-old obsolete version
of Pascal or BASIC ;-)

-- 
Scott Ribe
email@hidden
http://www.killerbytes.com/
(303) 722-0567 voice


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.