#if TRUE vs #if YES vs #if 1 are different from Objective-C?

In Xcode 6.1.1 (Obj-C)

#if 1
    NSLog(@"print 1");
#endif

#if TRUE
    NSLog(@"print TRUE");
#endif

#if YES
    NSLog(@"print YES");
#endif

      

And the result:

print 1
print TRUE

      

Can I explain the result? Why are #if TRUE

vs #if YES

vs #if 1

different?

+3


source to share


4 answers


Mhm ... I'm not really an objective-c person, I personally like C ++ better, but let me try to answer you anyway.

if 1

The compiler, after all, like everything in computers, runs on zeroes and in one. When you write an "if 1" statement in your code, it will always do it like any other number you can put there, that is, other than zero. This is because the bit representation of 0 in bytes is "00000000", which represents a negative value. Because of this, the most basic statement you can make to make sure you have true, at the code level and at the compiler level, is if (a nonzero number here) that will always be true.

if TRUE



true is a stored word in the compiler that eventually becomes 1. So if (true) always works; naturally, I guess it takes a while for the compiler to parse it, but that's pretty much the only difference, and it's pretty minor.

, If yes

The compiler does not know the word "Yes". Thus, it automatically accepts its parameter and tries to find if it was previously declared. When it discovers that you haven't defined it before in your program, it puts the default in the if statement - which is false; Thus, the command fails.

Hope I helped :)

+1


source


If a term is YES

either undefined or defined as 0

, it #if

will never become true.

If the identifier is not defined, it is treated as 0

. Therefore, the preprocessor will see

#if 0
    NSLog(@"print YES");
#endif

      



Which also won't execute the command NSLog

.

The code in the statement #if

will only be executed ifYES

  • identified and
  • set to non-zero
0


source


In preprocessor expressions for #if

all unknown identifiers are evaluated as 0

. So it seems that in your case TRUE

something is not defined 0

, but YES

is either undefined or defined with a value 0

.

0


source


It's hard to believe when you think about it, but the C programming language didn't have a boolean type until 1998. Programmers developed their own solutions, and over time it became common to use int with 0 for no. When the boolean was added at the end, it was like a true first class boolean type. This means that it can only store the values ​​0 and 1.read more here

You can check it out for yourself in objC with something like this.

bool foo = 55;
NSLog ((foo) ?  (@"foo, it is true") :(@"no, foo is false") ); //will log true..
NSLog (@"foo int value", (int)foo ); // will log 1 

      

now bool uses the full 8 bits, it's just (I think) that only the first (or the last depending on your / endian -ness architecture) is ever written to / read from.

ObjectiveC has now been running for much longer than 1998. So BOOL is actually older than bool! This is why it was always defined as char. It is actually capable of storing values ​​between -127 and 128. Again you can check this in Xcode

    BOOL bar = 55;
    NSLog ((bar) ?  (@"bar, it is true too!") :(@"no, bar neither") ); 
//will again log true..
    NSLog (@"bar int value", (int)bar ); 
// will log 55 on 32bit builds but 1 on 64bit builds 

      

Yes, but not always, as you can see. In 64-bit OBJC, BOOL is defined as bool!

from objc.h

 #if !defined(OBJC_HIDE_64) && TARGET_OS_IPHONE && __LP64__
    typedef bool BOOL;
    #else
    typedef signed char BOOL; 
    // BOOL is explicitly signed so @encode(BOOL) == "c" rather than "C" 
    // even if -funsigned-char is used.
    #endif

      

Because of bool and BOOL messy legacy in C and objC, truth tested for YES or true is not always 100% reliable. Instead, I recommend that you check for! False or! NO. Sounds funny?

heres a little blog about this i found on a big ranch nerd

PS I totally understand what you are talking about compiler conditions, but you marked it objC :)

0


source







All Articles