C/Macro Preprocessor/if def

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

#define and #ifdef

<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) || !defined (UKP)
   #error "RROR: NO_CURRENCY rate is specified."
  1. endif

main() {

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

}


      </source>


#define, #ifdef #else example

<source lang="cpp">

  1. include <stdio.h>
  2. define TED 10

int main(void) {

  1. ifdef TED
 printf("Hi Ted\n");
  1. else
 printf("Hi anyone\n");
  1. endif
  2. ifndef RALPH
 printf("RALPH not defined\n");
  1. endif
 return 0;

}

      </source>


Simple #if/#else example

<source lang="cpp">

  1. include <stdio.h>
  2. define MAX 10

int main(void) {

  1. if MAX>99
 printf("Compiled for array greater than 99.\n");
  1. else
 printf("Compiled for small array.\n");
  1. endif
 return 0;

}


      </source>


Simple #if example

<source lang="cpp">

  1. include <stdio.h>
  2. define MAX 100

int main(void) {

  1. if MAX>99
 printf("Compiled for array greater than 99.\n");
  1. endif
 return 0;

}

      </source>