C语言条件编译

2014-03-10 13:04:42 · 作者: · 浏览: 137

  C语言中的预编译包含三种:1.宏定义2.文件包含3.条件编译,条件编译指的是满足一定条件下才进行编译,它有几种形式:

  (1)#ifdef标识符

  //程序

  #else

  //程序

  #endif

  它的意义为如果定义了标识符,则执行程序段1,否则执行程序段2

  或者用以下的形式

  # ifdef 标识符

  //程序

  #endif

  # include <STDIO.H>

  # include <STDLIB.H>

  int main()

  {

  #ifdef DEBUG

  printf("debug is running\n");

  #else

  printf("debug is not running\n");

  #endif

  system("pause");

  return 0;

  }

  (2)

  #ifndef 标识符

  //程序1

  #else

  //程序2

  #endif

  它的含义是如果标识符没有被定义,则执行程序段1,否则执行程序段2

  # include <STDIO.H>

  # include <STDLIB.H>

  int main()

  {

  #ifndef DEBUG

  printf("debug is not running\n");

  #else

  printf("debug is  running\n");

  #endif

  system("pause");

  return 0;

  }

  (3)#if表达式

  //程序1

  #else

  //程序2

  #endif

  它的意义为表达式的值为真时,就编译程序段1,表达式的值为假时,就编译程序段2

  # include <STDIO.H>

  # include <STDLIB.H>

  # define HEX 1

  int main()

  {

  int i=10;

  #if HEX==1

  printf("%x\n",i);

  #else

  printf("%d\n",i);

  #endif

  system("pause");

  return 0;

  }