0042 条件编译

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


#include <stdio.h>

int main(int argc, const char * argv[]) {
   
    int score = 76;
    //判断成绩属于哪个等级
    if(score<60){
        printf("E\n");
    }else if (score<=69){
        printf("D\n");
    }else if (score<=79){
        printf("C\n");
    }else if (score<=89){
        printf("B\n");
    }else{
        printf("A\n");
    }
    
    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\n");
//    }else if (score<=69){
//        printf("D\n");
//    }else if (score<=79){
//        printf("C\n");
//    }else if (score<=89){
//        printf("B\n");
//    }else{
//        printf("A\n");
//    }
    
    //    int score = 76;
    //    //判断成绩属于哪个等级
    //    if(score<60){
    //        printf("E\n");
    //    }else if (score<=69){
    //        printf("D\n");
    //    }else if (score<=79){
    //        printf("C\n");
    //    }else if (score<=89){
    //        printf("B\n");
    //    }else{
    //        printf("A\n");
    //    }
    
    //    int score = 76;
    //    //判断成绩属于哪个等级
    //    if(score<60){
    //        printf("E\n");
    //    }else if (score<=69){
    //        printf("D\n");
    //    }else if (score<=79){
    //        printf("C\n");
    //    }else if (score<=89){
    //        printf("B\n");
    //    }else{
    //        printf("A\n");
    //    }
    
    
    //    int score = 76;
    //    //判断成绩属于哪个等级
    //    if(score<60){
    //        printf("E\n");
    //    }else if (score<=69){
    //        printf("D\n");
    //    }else if (score<=79){
    //        printf("C\n");
    //    }else if (score<=89){
    //        printf("B\n");
    //    }else{
    //        printf("A\n");
    //    }
    
#if score < 60
    printf("E\n");
#elif score <= 69
    printf("D\n");
#elif score <= 79
    printf("C\n");
#elif score <= 89
    printf("B\n");
#else
    printf("A\n");
#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\n",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\n",a,b);

}


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

int main(int argc, const char * argv[]) {
    
    //DEBUG1 == 1   显示调试信息
    //DEBUG1 == 0   不显示调试信息
    Log("xxxxxxxxxx-->\n");
    
    test1();
    test();
    
    return 0;
}
posted @ 2015-07-28 08:46  挨踢控  阅读(157)  评论(0编辑  收藏  举报