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