Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using #define in an "if" statement

Is it possible to use #define in an "if" statement? The following code works, but I get a warning that the macro is being redefined.

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
 #define TableViewHeight 916
 #define DisplayHeight 1024
 #define DisplayWidth 768
}
else {
 #define TableViewHeight 374
 #define DisplayHeight 480
 #define DisplayWidth 320
}

I also tried this, but it didn't work:

#ifdef UIUserInterfaceIdiomPad
 #define TableViewHeight 916
 #define DisplayHeight 1024
 #define DisplayWidth 768
#else
 #define TableViewHeight 374
 #define DisplayHeight 480
 #define DisplayWidth 320
#endif

Any ideas?

like image 487
Jonah Avatar asked Dec 28 '11 22:12

Jonah


People also ask

What is the synonym of using?

The words employ and utilize are common synonyms of use. While all three words mean "to put into service especially to attain an end," use implies availing oneself of something as a means or instrument to an end.

What is the verb of using?

Verb. use, employ, utilize mean to put into service especially to attain an end. use implies availing oneself of something as a means or instrument to an end. willing to use any means to achieve her ends employ suggests the use of a person or thing that is available but idle, inactive, or disengaged.

What does it mean by using someone?

transitive verb. If you say that someone uses people, you disapprove of them because they make others do things for them in order to benefit or gain some advantage from it, and not because they care about the other people. [disapproval]

What is the word for using what you have?

The words employ and use are common synonyms of utilize.


1 Answers

Yes, it's possible but it probably doesn't do what you think. Preprocessor directives are interpreted before the results of the preprocessing step are compiled.

This means that all of the preprocessor directives are interpreted, redefining some of the macros, before the remaining code, which will look something like below, is compiled.

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
}
else {
}

In other words, after preprocessing, you just have empty if and else bodies.

If you want to change the value of something based on a condition at run time then that something will have to be an genuine object and not just a preprocessor macro. E.g.

extern int TableViewHeight; // defined somewhere else
extern int DisplayHeight; // defined somewhere else
extern int DisplayWidth; // defined somewhere else

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  TableViewHeight = 916;
  DisplayHeight = 1024;
  DisplayWidth = 768;
}
else {
  TableViewHeight = 374;
  DisplayHeight = 480;
  DisplayWidth = 320;
}
like image 66
CB Bailey Avatar answered Nov 14 '22 07:11

CB Bailey