Chinaunix首页 | 论坛 | 博客
  • 博客访问: 36718
  • 博文数量: 18
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 95
  • 用 户 组: 普通用户
  • 注册时间: 2014-10-25 10:17
文章分类
文章存档

2015年(11)

2014年(7)

我的朋友

分类: C#/.net

2014-11-18 11:27:52

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


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


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


【例9-12】 
#include #define NUM ok int main(void){ struct stu{ int num; char *name; char sex; float score; } *ps; ps=(struct stu*)malloc(sizeof(struct stu)); ps->num=102; ps->name="Zhang ping"; ps->sex='M'; ps->score=62.5; #ifdef NUM printf("Number=%d\nScore=%f\n",ps->num,ps->score); #else printf("Name=%s\nSex=%c\n",ps->name,ps->sex); #endif free(ps); return 0; }#include
#define NUM ok
int main(void){
    struct stu{
        int num;
        char *name;
        char sex;
        float score;
    } *ps;
    ps=(struct stu*)malloc(sizeof(struct stu));
    ps->num=102;
    ps->name="Zhang ping";
    ps->sex='M';
    ps->score=62.5;
    #ifdef NUM
        printf("Number=%d\nScore=%f\n",ps->num,ps->score);
    #else
        printf("Name=%s\nSex=%c\n",ps->name,ps->sex);
    #endif
    free(ps);
    return 0;
}由于在程序的第14行插入了条件编译预处理命令,因此要根据NUM是否被定义过来决定编译那一个printf语句。而在程序的第一行已对NUM作过宏定义,因此应对第一个printf语句作编译故运行结果是输出了学号和成绩。


在程序的第1行宏定义中,定义NUM表示字符串OK,其实也可以为任何字符串,甚至不给出任何字符串,写为: 
#define NUM#define NUM也具有同样的意义。只有取消程序的第1行才会去编译第二个printf语句。读者可上机试作。


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


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


【例9-13】 
#include #define R 1 int main(void){ float c,r,s; printf ("input a number: "); scanf("%f",&c); #if R r=3.14159*c*c; printf("area of round is: %f\n",r); #else s=c*c; printf("area of square is: %f\n",s); #endif return 0; }#include
#define R 1
int main(void){
    float c,r,s;
    printf ("input a number:  ");
    scanf("%f",&c);
    #if R
        r=3.14159*c*c;
        printf("area of round is: %f\n",r);
    #else
        s=c*c;
        printf("area of square is: %f\n",s);
    #endif
    return 0;
}本例中采用了第三种形式的条件编译。在程序第1行宏定义中,定义R为1,因此在条件编译时,常量表达式的值为真,故计算并输出圆面积。


上面介绍的条件编译当然也可以用条件语句来实现。 但是用条件语句将会对整个源程序进行编译,生成的目标代码程序很长,而采用条件编译,则根据条件只编译其中的程序段1或程序段2,生成的目标程序较短。如果条件选择的程序段很长,采用条件编译的方法是十分必要的。
推荐阅读:http://blog.csdn.net/qq_22878361/article/details/41205673

阅读(541) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~