C/Macro Preprocessor/Preprocessor IF

Материал из C\C++ эксперт
Перейти к: навигация, поиск

IF and endif preprocessor

<source lang="cpp">

  1. include <stdio.h>
  2. define TEST 29
  3. if defined TEST

int main(void) {

 printf("This is a test.\n");
 return 0;

}

  1. endif


      </source>


ifdef preprocessor

<source lang="cpp">

  1. include <stdio.h>
  2. define DOG

int main(void) {

  1. ifdef DOG
 printf("DOG is defined.\n");
  1. endif
  2. undef DOG
  3. ifdef DOG
 printf("This line is not compiled.\n");
  1. endif
 return 0;

}


      </source>


#IF #ELSE #ENDIF

<source lang="cpp"> //file1.h

  1. define USD 1

//file2.h

  1. define UKP 1

//file3

  1. include <stdio.h>
  2. include <file1.h>
  3. if (defined (USD))
   #define currency_rate 46
  1. else
   #define currency_rate 100 
  1. endif

main() {

 int rs;
 rs = 10 * currency_rate;
 printf ("d\n", rs);

}


      </source>