Reputation: 12367
I have defined to preprocessor directives:
#define PTM_RATIO 32
#define PTM_RATIO_2 isIPad?64:32
Then inside a method I use the above directives like this:
NSLog(@"PTM_RATIO %d",PTM_RATIO);
NSLog(@"PTM_RATIO_2 %d",PTM_RATIO_2);
float32 test=100*PTM_RATIO;
float32 test2=100*PTM_RATIO_2;
NSLog(@"Test %f",test);
NSLog(@"Test2 %f",test2);
Here's the output of the above code:
2013-03-25 23:43:57.011 Box2d[3625:15203] PTM_RATIO 32
2013-03-25 23:43:57.019 Box2d[3625:15203] PTM_RATIO_2 32
2013-03-25 23:43:57.020 Box2d[3625:15203] Test 3200.000000
2013-03-25 23:43:57.021 Box2d[3625:15203] Test2 32.000000
As you can see, both PTM_RATIO and PTM_RATIO_2 are 32. Then why do I get 3200.000000 when 100 is multiplied by PTM_RATIO, but 32.000000 when it's multiplied by PTM_RATIO_2?
Upvotes: 1
Views: 54
Reputation: 7065
The PTM_RATIO_2 is evaluated as follows in real code
float32 test2 = (100*isIPad)?64:32;
You are assuming that the MACRO will evaluate prior to live code calculation.
Upvotes: 1
Reputation: 222244
Preprocessor macros are largely text substitutions. Change:
#define PTM_RATIO_2 isIPad?64:32
to:
#define PTM_RATIO_2 (isIPad?64:32)
Without that, your source code:
100*PTM_RATIO_2
expanded to:
100*isIPad?64:32
which is then parsed as:
(100*isIPAD) ? 64 : 32
Upvotes: 3