设为首页 加入收藏

TOP

C语言条件编译
2014-03-10 13:04:42 来源: 作者: 【 】 浏览:133
Tags:语言 条件 编译

  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;

  }

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C指针原理:json-glib剖析 下一篇c指针(29):C应用技巧

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: