Objective-C TRUE / FALSE vs true / false

In Objective-C, we have different keywords for evaluating Boolean values. We have YES/NO , TRUE/FALSE and (c99) true/false . I understand BOOL vs BOOL , and this article perfectly explains the differences:

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

So what YES/NO means is not my question. My question is, what is the difference between TRUE/FALSE and TRUE/FALSE ? Is uppercase alias for YES/NO or alias for (c99) true/false ? Or is it another animal in general (e.g. YES/NO )?

+5
source share
2 answers

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

The identifiers true and false are standard C99 (as you noted) if you #include <stdbool.h> . Note that since you are using Objective-C, you probably include stdbool.h indirectly, even if you do not know it. For example, Foundation.h includes CoreFoundation.h , which includes stdbool.h . Thus, it is quite difficult to compile an iOS or Mac application without getting true and false .

The identifiers true and false are not standard. This is historical baggage identified by various libraries. Libraries may have been written before C99 or written after, but are designed to support pre-C99 compilers or simply written by authors unfamiliar with C99 boolean literals. On Mac, some examples of such libraries are Kerberos, XDR / RPC, and ncurses. Most importantly, Mach kernel headers define the constants true and false , and (as is the case with stdbool.h ) it’s quite difficult to avoid these specific definitions if you are building a modern application for iOS or Mac.

In all cases that I could find, true is defined as 1 or (1) , and false is defined as 0 or (0) .

(All the libraries I mentioned are the Mach core, which precedes C99, and is thus justified in defining its own Boolean constants.)

+15
source

For ncurses, the use of TRUE / FALSE / bool precedes c99 and has been documented in XPG4 Curses since 1996. The ncurses' configure script checks and uses existing C ++ and c99 values ​​when available. See, for example, the ncurses change log starting in early 1997.

+1
source

Source: https://habr.com/ru/post/1210064/


All Articles