******************** 条件编译指令 ********************

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------




  条件编译指令


条件编译指令:


第一种形式:


#if           ---> 条件满足的时候,执行if代码块

if代码块


#elif


#else


#endif



第二种形式:


#ifdef  xxx      ----> 判断是否定义了 xxx 这个宏


#elif


#else


#endif


第三种形式:


#ifndef  xxxx      ---->  if not define 如果没有定义xxxx,

就编译代码段1


代码段1;


#elif


#else


#endif



*/


#include <stdio.h>



//ifdef 的使用

void test(){

    int num =0;

#ifdef DEBUG1       //如果系统已经定义了一个宏DEBUG1就执行代码块1

    

    num = 1;

    

#else 

    num = -1;

#endif

    //1 说明DEBUG1 这个宏已经被定义了,-1说明DEBUG1 这个宏没有定义

    printf("num = %d\n",num);

}



int main(int argc, const char * argv[]) {

    

    int num = 0;

#ifndef DEBUG 

    num = 1;

    

#else

    num = -1;

    

#endif

    //test();

    //1说明DEBUG 这个宏没有定义

    //-1说明DEBUG这个宏已经定义

    printf("num = %d\n",num);

    return 0;

}



//  条件编译的概念及优点


//  if else 的优化

//  节省内存空间,提高效率

//  条件编译指令: 当条件满足的时候,编译这部分,否则编译else部分

//  此处条件 score 是一个宏

#include <stdio.h>

//#define score 77


int main(int argc, const char * argv[]) {

    int score;


    while(1){

        //提示用户输入

        printf("请输入分数\n");

        //接收输入

        scanf("%d",&score);

        if(score>=0 &&score <=100)break;

    }

    

#if score < 60          //if

    printf("E\n");

    

#elif score < 70        //else if

    printf("D\n");

    

#elif score < 80

    printf("C\n");

    

#elif score < 90

    printf("B\n");

    

#else

    printf("A\n");

    

#endif

    

    

    return 0;

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值