手机版
你好,游客 登录 注册
背景:
阅读新闻

C语言条件编译

[日期:2014-02-24] 来源:Linux社区  作者:Linux [字体:大 中 小]

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;
}

linux
相关资讯       C语言  条件编译 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款