• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: ObjC BOOL and boolean c expressions
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ObjC BOOL and boolean c expressions


  • Subject: Re: ObjC BOOL and boolean c expressions
  • From: Scott Ribe <email@hidden>
  • Date: Wed, 05 Sep 2007 21:03:35 -0600
  • Thread-topic: 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:

This email sent to email@hidden

  • Follow-Ups:
    • Re: ObjC BOOL and boolean c expressions
      • From: glenn andreas <email@hidden>
  • Prev by Date: Re: CoreData - cannot load .sqlstore if path contains Alias folder
  • Next by Date: Re: ObjC BOOL and boolean c expressions
  • Previous by thread: Blogo weblog editor - Beta
  • Next by thread: Re: ObjC BOOL and boolean c expressions
  • Index(es):
    • Date
    • Thread