0042 条件编译

/*
 
   条件编译:
 
      发生在预处理阶段,在编译之前做的事情
 
      核心:根据条件编译指定的代码
 
      条件不同,编译的部分也不同,生成的目标文件(.o)大小也不一样
 
 
 
 
 
 
 
 
 
 */


#include <stdio.h>

int main(int argc, const char * argv[]) {
   
    int score = 76;
    //判断成绩属于哪个等级
    if(score<60){
        printf("E
");
    }else if (score<=69){
        printf("D
");
    }else if (score<=79){
        printf("C
");
    }else if (score<=89){
        printf("B
");
    }else{
        printf("A
");
    }
    
    return 0;
}
//
//  main.c
//  20-#if-#else 条件编译指令
//
//  Created by apple on 15/1/11.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#include <stdio.h>
#define score 99

int main(int argc, const char * argv[]) {
   
    //传统方式
//    int score = 76;
//    //判断成绩属于哪个等级
//    if(score<60){
//        printf("E
");
//    }else if (score<=69){
//        printf("D
");
//    }else if (score<=79){
//        printf("C
");
//    }else if (score<=89){
//        printf("B
");
//    }else{
//        printf("A
");
//    }
    
    //    int score = 76;
    //    //判断成绩属于哪个等级
    //    if(score<60){
    //        printf("E
");
    //    }else if (score<=69){
    //        printf("D
");
    //    }else if (score<=79){
    //        printf("C
");
    //    }else if (score<=89){
    //        printf("B
");
    //    }else{
    //        printf("A
");
    //    }
    
    //    int score = 76;
    //    //判断成绩属于哪个等级
    //    if(score<60){
    //        printf("E
");
    //    }else if (score<=69){
    //        printf("D
");
    //    }else if (score<=79){
    //        printf("C
");
    //    }else if (score<=89){
    //        printf("B
");
    //    }else{
    //        printf("A
");
    //    }
    
    
    //    int score = 76;
    //    //判断成绩属于哪个等级
    //    if(score<60){
    //        printf("E
");
    //    }else if (score<=69){
    //        printf("D
");
    //    }else if (score<=79){
    //        printf("C
");
    //    }else if (score<=89){
    //        printf("B
");
    //    }else{
    //        printf("A
");
    //    }
    
#if score < 60
    printf("E
");
#elif score <= 69
    printf("D
");
#elif score <= 79
    printf("C
");
#elif score <= 89
    printf("B
");
#else
    printf("A
");
#endif
    
    return 0;
}
/*
 
  条件编译指令
 
    1) #if  #elif  #else  #endif
 
 
 
    2) #ifdef  用来判断某个宏是否定义
 
 
 
 
 
 
 
 
 */


#include <stdio.h>
#define DEBUG1 1
#define DEBUG2 0

int main(int argc, const char * argv[]) {
    
    int a = 0;
    //ifdef检测宏是否定义
    #ifdef DEBUG1   //DEBUG 系统已经定义了这个宏了
       a = 10;
    #else 
       a = 10000;
    #endif
    
    //ifndef 检测宏是否定义    ifndef 如果没有定义
    
    #ifndef DEBUG2
    a = 100;
    #else
    a = -1;
    #endif
    
    printf("%d
",a);
    
    
    return 0;
}
//
//  main.c
//  22-使用条件编译指令调试bug
//
//  Created by apple on 15/1/11.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#include <stdio.h>

#define DEBUG1 1

#if DEBUG1 == 1
//显示调试信息
#define Log(format,...) printf(format,## __VA_ARGS__)

#else
//不显示调试信息
#define Log(format,...)

#endif

void test(){

    int a  = 10,b=3;
    Log("test--->%d,%d
",a,b);

}


void test1(){
    
    printf("xxxxx
");
    float b  = 10.23f;
    Log("test1--->%.2f
",b);
    
}

int main(int argc, const char * argv[]) {
    
    //DEBUG1 == 1   显示调试信息
    //DEBUG1 == 0   不显示调试信息
    Log("xxxxxxxxxx-->
");
    
    test1();
    test();
    
    return 0;
}
原文地址:https://www.cnblogs.com/aiti/p/4681771.html