Code below yields the output “yes defined”, “no defined” and “yes”. Why?
#define FOOBAR NO
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
#ifdef YES
NSLog(@"yes defined");
#endif
#ifdef NO
NSLog(@"no defined");
#endif
#if FOOBAR == YES
NSLog(@"yes");
#else
NSLog(@"no");
#endif
// ...
}
YES and NO are not undefined, objc.h defines them as:
typedef signed char BOOL;
#define YES (BOOL)1
#define NO (BOOL)0
What is the value of
NO? If it’s undefined (likeYES), they will both evaluate to 0.This means your expression is essentially
which is of course true, and thus causes the first call to be compiled.
UPDATE: Not sure how
BOOLis defined, but casting to what might be atypedef:ed type is not a very good idea when dealing with the preprocessor. Remember that the the#ifis evaluated by the preprocessor, not by the compiler. Read something like this for more information about expressions in the preprocessor. Especially: