user1068477
user1068477

Reputation:

Objective-C TRUE/FALSE vs true/false

In Objective-C we have different keywords to evaluate boolean values. We have YES/NO, TRUE/FALSE and (c99) true/false. I understand BOOL vs bool well enough and this article explains the differences wonderfully:

http://www.bignerdranch.com/blog/bools-sharp-corners/

So what YES/NO means isn't my question. My question is, what is the difference between TRUE/FALSE and true/false? Is the uppercase version an alias for YES/NO or an alias for (c99) true/false? Or, is it another animal altogether (like YES/NO) ?

Upvotes: 6

Views: 6091

Answers (2)

rob mayoff
rob mayoff

Reputation: 385998

The YES and NO identifiers are considered the standard Objective-C literals for BOOL. You usually won't find YES, NO, or BOOL outside of Objective-C source code. Note that these identifiers are actually macros defined in objc/objc.h.

The true and false identifiers are standard C99 (as you noted), if you #include <stdbool.h>. Note that, since you're using Objective-C, you are probably including stdbool.h indirectly, even if you don't know it. For example, Foundation.h includes CoreFoundation.h, which includes stdbool.h. Thus it's pretty hard to compile a modern iOS or Mac app without getting true and false.

The TRUE and FALSE identifiers are not standard. They are historic baggage defined by various libraries. The libraries may have been written before the advent of C99, or written after but intended to support pre-C99 compilers, or simply written by authors ignorant of the C99 boolean literals. On the Mac, some examples of such libraries are Kerberos, XDR/RPC, and ncurses. Most importantly, the Mach kernel headers define TRUE and FALSE constants, and (as with stdbool.h) it's pretty hard to avoid these particular definitions if you're building a modern iOS or Mac app.

In all the cases I could find, TRUE is defined as 1 or (1) and FALSE is defined as 0 or (0).


All of the libraries I mentioned, and the Mach kernel, predate C99 and are thus justified in defining their own boolean constants.

Objective-C's BOOL, YES, and NO appear in Brad Cox's Object-Oriented Programming: An Evolutionary Approach from 1991, so these identifiers are also justified.

Upvotes: 18

Thomas Dickey
Thomas Dickey

Reputation: 54583

With regard to ncurses, the TRUE/FALSE/bool usage predates c99 and is documented in XPG4 Curses from 1996. ncurses' configure script checks for and uses the existing c++ and c99 values when they are available. See for example the ncurses change-log starting in early 1997

Upvotes: 1

Related Questions