码迷,mamicode.com
首页 > 编程语言 > 详细

C语言条件编译详解

时间:2015-04-27 13:06:12      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

预处理程序提供了条件编译的功能。可以按不同的条件去编译不同的程序部分,因而产生不同的目标代码文件。这对于程序的移植和调试是很有用的。条件编译有三种形式,下面分别介绍。

第一种形式

第一种形式的格式为:
    #ifdef  标识符
        程序段1
    #else
        程序段2
    #endif

它的功能是,如果标识符已被 #define命令定义过则对程序段1进行编译;否则对程序段2进行编译。如果没有程序段2(它为空),本格式中的#else可以没有,即可以写为:
    #ifdef  标识符
        程序段
    #endif

【例9-12】

  1. #include <stdio.h>
  2. #define NUM ok
  3. int main(void){
  4. struct stu{
  5. int num;
  6. char *name;
  7. char sex;
  8. float score;
  9. } *ps;
  10. ps=(struct stu*)malloc(sizeof(struct stu));
  11. ps->num=102;
  12. ps->name="Zhang ping";
  13. ps->sex=‘M‘;
  14. ps->score=62.5;
  15. #ifdef NUM
  16. printf("Number=%d\nScore=%f\n",ps->num,ps->score);
  17. #else
  18. printf("Name=%s\nSex=%c\n",ps->name,ps->sex);
  19. #endif
  20. free(ps);
  21. return 0;
  22. }

由于在程序的第14行插入了条件编译预处理命令,因此要根据NUM是否被定义过来决定编译那一个printf语句。而在程序的第一行已对NUM作过宏定义,因此应对第一个printf语句作编译故运行结果是输出了学号和成绩。

在程序的第1行宏定义中,定义NUM表示字符串OK,其实也可以为任何字符串,甚至不给出任何字符串,写为:

  1. #define NUM

也具有同样的意义。只有取消程序的第1行才会去编译第二个printf语句。读者可上机试作。

第二种形式

第二种形式的格式为:
    #ifndef 标识符
        程序段1 
    #else 
        程序段2 
    #endif
与第一种形式的区别是将“ifdef”改为“ifndef”。它的功能是,如果标识符未被#define命令定义过则对程序段1进行编译,否则对程序段2进行编译。这与第一种形式的功能正相反。


http://www.zbnews.net/jiankang/list1/234895.html
http://www.zbnews.net/jiankang/list1/234894.html
http://www.zbnews.net/jiankang/list1/234891.html
http://www.51etong.com/jkzt/list1/234889.html
http://www.zbnews.net/jiankang/list1/234887.html
http://www.51etong.com/jkzt/list1/234886.html
http://www.zbnews.net/jiankang/list1/234885.html
http://www.zbnews.net/jiankang/list1/234884.html
http://www.zbnews.net/jiankang/list1/234882.html
http://www.51etong.com/jkzt/list1/234880.html
http://www.lznews.gov.cn/uzt/list1/234879.html
http://www.lznews.gov.cn/uzt/list1/234878.html
http://www.51etong.com/jkzt/list1/234876.html
http://www.lznews.gov.cn/uzt/list1/234875.html
http://www.lznews.gov.cn/uzt/list1/234874.html
http://www.lznews.gov.cn/uzt/list1/234873.html
http://www.zbnews.net/jiankang/list1/234872.html
http://www.51etong.com/jkzt/list1/234871.html
http://www.51etong.com/jkzt/list1/234870.html
http://www.51etong.com/jkzt/list1/234867.html
http://user.qzone.qq.com/2633883405
http://t.qq.com/changchuny8250
http://health.people.com.cn/xywy/wfyxb/tp/8634716732.html
http://health.people.com.cn/xywy/wfyxb/tp/8634711498.html
http://health.people.com.cn/xywy/wfyxb/tp/8634708130.html
http://health.people.com.cn/xywy/wfyxb/tp/8634705327.html
http://health.people.com.cn/xywy/wfyxb/tp/8634702557.html
http://health.people.com.cn/xywy/wfyxb/tp/8634699443.html
http://health.people.com.cn/xywy/wfyxb/tp/8634695041.html
http://health.people.com.cn/xywy/wfyxb/tp/8634692366.html
http://health.people.com.cn/xywy/wfyxb/tp/8634689457.html
http://health.people.com.cn/xywy/wfyxb/tp/8634686655.html
http://health.people.com.cn/xywy/wfyxb/npxpf/8634680004.html
http://health.people.com.cn/xywy/wfyxb/npxpf/8634675843.html
http://health.people.com.cn/xywy/wfyxb/npxpf/8634671613.html
http://health.people.com.cn/xywy/wfyxb/npxpf/8634668953.html
http://health.people.com.cn/xywy/wfyxb/npxpf/8634665768.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8634662151.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8634659390.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8634655387.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8634652223.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8634648791.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8634645687.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8634642791.html
http://health.people.com.cn/xywy/wfyxb/zl/8634639673.html
http://health.people.com.cn/xywy/wfyxb/zl/8634636341.html
http://health.people.com.cn/xywy/wfyxb/zl/8634633176.html
http://health.people.com.cn/xywy/wfyxb/zl/8634629876.html
http://health.people.com.cn/xywy/wfyxb/zl/8634626116.html
http://health.people.com.cn/xywy/wfyxb/zl/8634621396.html
http://health.people.com.cn/xywy/wfyxb/zl/8634618202.html
http://health.people.com.cn/xywy/wfyxb/zl/8634615071.html
http://health.people.com.cn/xywy/wfyxb/zl/8634612138.html
http://health.people.com.cn/xywy/wfyxb/kfal/8634608141.html
http://health.people.com.cn/xywy/wfyxb/kfal/8634601130.html
http://health.people.com.cn/xywy/wfyxb/kfal/8634598198.html
http://health.people.com.cn/xywy/wfyxb/kfal/8634595171.html
http://health.people.com.cn/xywy/wfyxb/kfal/8634591848.html
http://health.people.com.cn/xywy/wfyxb/kfal/8634587855.html
http://health.people.com.cn/xywy/wfyxb/kfal/8634584673.html
http://health.people.com.cn/xywy/wfyxb/kfal/8634580296.html
http://health.people.com.cn/xywy/wfyxb/kfal/8634576927.html
http://health.people.com.cn/xywy/wfyxb/kfal/8634572513.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8634568629.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8634565548.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8634561012.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8634558160.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8634555287.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8634551027.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8634546991.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8634543966.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8634540202.html
http://health.people.com.cn/xywy/wfyxb/npxpf/8665825627.html
http://health.people.com.cn/xywy/wfyxb/npxpf/8665803242.html
http://health.people.com.cn/xywy/wfyxb/npxpf/8665760857.html
http://health.people.com.cn/xywy/wfyxb/npxpf/8665738541.html
http://health.people.com.cn/xywy/wfyxb/npxpf/8665725796.html
http://health.people.com.cn/xywy/wfyxb/npxpf/8665705278.html
http://health.people.com.cn/xywy/wfyxb/npxpf/8665676307.html
http://health.people.com.cn/xywy/wfyxb/tp/8665652613.html
http://health.people.com.cn/xywy/wfyxb/tp/8665634968.html
http://health.people.com.cn/xywy/wfyxb/tp/8665618204.html
http://health.people.com.cn/xywy/wfyxb/tp/8665599559.html
http://health.people.com.cn/xywy/wfyxb/tp/8665570637.html
http://health.people.com.cn/xywy/wfyxb/tp/8665546616.html
http://health.people.com.cn/xywy/wfyxb/tp/8665516148.html
http://health.people.com.cn/xywy/wfyxb/tp/8665494052.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8665348011.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8665332141.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8665304778.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8665279337.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8665245471.html
http://health.people.com.cn/xywy/wfyxb/wfyxb/8665217641.html
http://health.people.com.cn/xywy/wfyxb/zl/8665183707.html
http://health.people.com.cn/xywy/wfyxb/zl/8665151882.html
http://health.people.com.cn/xywy/wfyxb/zl/8665126913.html
http://health.people.com.cn/xywy/wfyxb/zl/8665095276.html
http://health.people.com.cn/xywy/wfyxb/zl/8665065223.html
http://health.people.com.cn/xywy/wfyxb/zl/8665044459.html
http://health.people.com.cn/xywy/wfyxb/zl/8665016966.html
http://health.people.com.cn/xywy/wfyxb/zl/8664994329.html
http://health.people.com.cn/xywy/wfyxb/zl/8664964107.html
http://health.people.com.cn/xywy/wfyxb/zl/8664946776.html
http://health.people.com.cn/xywy/wfyxb/kfal/8664904496.html
http://health.people.com.cn/xywy/wfyxb/kfal/8664875723.html
http://health.people.com.cn/xywy/wfyxb/kfal/8664841175.html
http://health.people.com.cn/xywy/wfyxb/kfal/8664820153.html
http://health.people.com.cn/xywy/wfyxb/kfal/8664800127.html
http://health.people.com.cn/xywy/wfyxb/kfal/8664786867.html
http://health.people.com.cn/xywy/wfyxb/kfal/8664757276.html
http://health.people.com.cn/xywy/wfyxb/kfal/8664738646.html
http://health.people.com.cn/xywy/wfyxb/kfal/8664718323.html
http://health.people.com.cn/xywy/wfyxb/kfal/8664688557.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8664668308.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8664646362.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8664631785.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8664603483.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8664580771.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8664560208.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8664545675.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8664532567.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8664505390.html
http://health.people.com.cn/xywy/wfyxb/yyxw/8664484808.html

第三种形式

第三种形式的格式为:
    #if 常量表达式
        程序段1
    #else 
        程序段2
    #endif
它的功能是,如常量表达式的值为真(非0),则对程序段1 进行编译,否则对程序段2进行编译。因此可以使程序在不同条件下,完成不同的功能。

【例9-13】

  1. #include <stdio.h>
  2. #define R 1
  3. int main(void){
  4. float c,r,s;
  5. printf ("input a number: ");
  6. scanf("%f",&c);
  7. #if R
  8. r=3.14159*c*c;
  9. printf("area of round is: %f\n",r);
  10. #else
  11. s=c*c;
  12. printf("area of square is: %f\n",s);
  13. #endif
  14. return 0;
  15. }

本例中采用了第三种形式的条件编译。在程序第1行宏定义中,定义R为1,因此在条件编译时,常量表达式的值为真,故计算并输出圆面积。

C语言条件编译详解

标签:

原文地址:http://www.cnblogs.com/guguniao/p/4459684.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!