blogger Indonesia

Follow judhyns blog

#if

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

  1. Statement B indicates the if directive.

  2. The generalized form of the if directive is

    #if  #endif 
  3. The condition (1>0) is absolutely not necessary here. It is given just to indicate how you can concatanate multiple conditions.

  4. The condition defined (USD) is true only if the identifier USD is defined.

  5. Since file1.h is included and USD gets defined, the condition is evaluated as true and the currency rate is defined as 46.

  6. In statement E, the condition is false because UKP is not defined.

  7. In the statement H, the currency rate is 46.

Points to Remember

  1. The if directive allows us to use a condition more generalized than ifdef.

  2. The defined() predicate returns true if the symbol is defined; otherwise, it is false.

No comments:

feed

PR Check

activesearchresults

judhyn's blog

 

http://www.judhyn.blogspot.com | |