Re: BOOLs, definition of YES
Re: BOOLs, definition of YES
- Subject: Re: BOOLs, definition of YES
- From: Alexei Kosut <email@hidden>
- Date: Tue, 20 May 2003 12:51:12 -0500
On Tuesday, May 20, 2003, at 09:01 AM, Tom Sutcliffe wrote:
Sorry this is a minor Java/C types whinge but I'm interested in how
others approach this.
Coming from Java I'm used to having booleans as a distinct type, and
for the ! operator to work correctly on them. So in Obj-C I was
surprised to find that because of YES and NO being defined as 1 and 0,
YES doesn't equal !!YES.
I'm therefore interested in how best to go about inverting a BOOL
variable.
Others have commented on what YES, NO, !YES and !NO are (1, 0, 0 and 1)
, but I will point out that any non-zero integer (or pointer, or
floating point) value evaluates to true in a boolean context. So YES
could be 1, it could be 27 or it could be -14, and everything would
still work out okay. Since !0 is nonzero and applying ! to any
non-zero value gives you 0, it really doesn't matter what YES is, as
long as it's non-zero.
However, note that you should absolutely never compare to YES, since
there are non-false values that are not equal to YES. Really, there's
no need to compare to NO either (just use (expr) or (!expr)), but (expr
!= NO) and (expr == NO) are safe, if you want to be clear.
For more on C's boolean types (or rather, the lack thereof), see
<
http://www.eskimo.com/~scs/C-faq/s9.html>
P.S. It is safe to compare to the C++/C99 "true" value, since that
forces a boolean context, where the only legal values are false (0) and
true (1).
--
Alexei Kosut <email@hidden> <
http://cs.stanford.edu/~akosut/>
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.