• 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: John Stiles <email@hidden>
  • Date: Fri, 31 Aug 2007 10:02:09 -0700

I *think* that the C spec doesn't specify the result of a boolean expression other than "zero means false, nonzero means true." So from a "compliance with the spec" standpoint, the first version might return different results from the second version.

In practice, I've never seen a compiler which didn't generate 1 for a true result when computing a boolean expression. And YES == 1.

I am sure that an exception exists out there somewhere, on some weird hardware/compiler combination, where a true boolean expression returns a value other than one, but I doubt you will see this becoming an issue in your code if you keep using standard tools.


On Aug 30, 2007, at 11:19 PM, Charles Steinman wrote:


--- Eyal Redler <email@hidden> wrote:

Hi,

I'm not sure where and when but I recall reading
something about ObjC
BOOL type values YES and NO not being the same as
whet a boolean
expression might produce.
I can't recall the exact details but there was some
kind of gotcha
related to this. For example, the expression

BOOL myBool=(myInt==1);

might set myBool differently then

BOOL myBool=(myInt==1)?YES:NO;

The two expressions are equivalent. YES is defined as (BOOL)1, while NO is defined as (BOOL)0.


Or maybe it was that

if (myBool)
	[foo bar];

will perform differently then
if (myBool==YES)
	[foo bar];

I wouldn't say it will work differently, but it might. There's no language constraint that says a BOOL can only be set to YES or NO (though I wouldn't intentionally set a BOOL to some other value). So if a method returns a nonzero value that is not 1, if (myBool) will evaluate to true while if (myBool == YES) will evaluate to false.

Cheers,
Chuck



______________________________________________________________________ ______________
Got a little couch potato?
Check out fun summer activities for kids.
http://search.yahoo.com/search?fr=oni_on_mail&p=summer+activities +for+kids&cs=bz
_______________________________________________


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:
40blizzard.com


This email sent to email@hidden

_______________________________________________

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: Charles Steinman <email@hidden>
    • Re: ObjC BOOL and boolean c expressions
      • From: "Clark Cox" <email@hidden>
References: 
 >Re: ObjC BOOL and boolean c expressions (From: Charles Steinman <email@hidden>)

  • Prev by Date: Re: Who Calls Accessors in Bookmarks?
  • Next by Date: Per Process HTTP Proxy
  • Previous by thread: Re: ObjC BOOL and boolean c expressions
  • Next by thread: Re: ObjC BOOL and boolean c expressions
  • Index(es):
    • Date
    • Thread