Introduction
#if allows you to define more generalized conditions. Multiple conditions, which are connected by relational operators such as AND(&&), OR(||), are allowed.
Program
Suppose we have three files:
file1.h #define USD 1 file2.h #define UKP 1 file3 #include#include //A #if ((1>0) &&(defined (USD)) // B #define currency_rate 46 //C #endif //D #if (defined (UKP)) // E #define currency_rate 100 //F #endif //G main() { int rs; rs = 10 * currency_rate; //H printf ("%d\n", rs); }
Explanation
-
Statement B indicates the if directive.
-
The generalized form of the if directive is
#if
#endif -
The condition (1>0) is absolutely not necessary here. It is given just to indicate how you can concatanate multiple conditions.
-
The condition defined (USD) is true only if the identifier USD is defined.
-
Since file1.h is included and USD gets defined, the condition is evaluated as true and the currency rate is defined as 46.
-
In statement E, the condition is false because UKP is not defined.
-
In the statement H, the currency rate is 46.
Points to Remember
-
The if directive allows us to use a condition more generalized than ifdef.
-
The defined() predicate returns true if the symbol is defined; otherwise, it is false.
No comments:
Post a Comment