C/Macro Preprocessor/if def

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

#define and #ifdef

//file1.h
#define USD 1
//file2.h
#define UKP 1
//file3
#include <stdio.h>
#include <file1.h>
#if !defined (USD) || !defined (UKP)    
    #error "RROR: NO_CURRENCY rate is specified."
#endif
main() {    
    
  int rs;
  rs = 10 * currency_rate;
  printf ("d\n", rs);
}


#define, #ifdef #else example

#include <stdio.h>
#define TED 10
int main(void)
{
#ifdef TED
  printf("Hi Ted\n");
#else
  printf("Hi anyone\n");
#endif
#ifndef RALPH
  printf("RALPH not defined\n");
#endif
  return 0;
}


Simple #if/#else example

#include <stdio.h>
#define MAX 10
int main(void)
{
#if MAX>99
  printf("Compiled for array greater than 99.\n");
#else
  printf("Compiled for small array.\n");
#endif 
  return 0;
}


Simple #if example

#include <stdio.h>
#define MAX 100
int main(void)
{
#if MAX>99
  printf("Compiled for array greater than 99.\n");
#endif
  return 0;
}