zoukankan      html  css  js  c++  java
  • C语言学习11:typedef和define区别,头文件的使用以及 <>和“”区别,宏定义中#和##的用法,条件编译,gcc和c99中宏定义使用printf和sprintf,条件编译避免头文件包含

    1,typedef和define区别

    #include <stdio.h> 
    #define INT32 int 
    #define COUNT 10 
    
    //typedef 是编译器关键字,和#define是完全不同的 
    //typede 是会令编译器做类型推导, 
    //#define 是预处理关键字,预处理后即处理完毕 
    typedef int int32; 
    //带参数宏(按照参数进行宏替换) 
    #define max(a,b) (((a)>(b))?(a):(b)) 
    
    int main(void) 
    { 
        int i; 
        INT32 a=3; 
        int32  b=5; 
        int m; 
    
        for(i=0;i<COUNT;i++) 
            printf("%d ",i); 
        putchar('
    '); 
    
        m=max(a+3,b-1); 
        printf("m=%d
     ",m); 
    
        return 0; 
    } 

    结果:

    will@will-laptop:~/ex/10$ ./a.out 
    0 1 2 3 4 5 6 7 8 9 
    m=6 

    2,头文件的使用以及<>和“”的区别

    C文件:
    //#include<>搜索系统默认目录(/usr/include  /usr/local/include  gcc指定目录) 
    //#include“”搜索源文件当前目录,然后搜索系统默认目录 
    //<>或者“”内是头文件路径(相对路径 /绝对路径) 
    #include </usr/include/stdio.h> 
    #include "headerfile/123.h" 
    
    int main(void) 
    { 
        printf("hello!
    "); 
        printf("max=%d
    ",max(6,7)); 
        return 0; 
    }
    H文件:123.h
    #define max(a,b) ( (a>b)?(a):(b))

    结果:

    will@will-laptop:~/ex/10$ ./a.out 
    hello! 
    max=7 

    3,宏定义中#和##的用法

    #include<stdio.h> 
    //#将传入参数名转换成加上“”的参数名字符串 
    #define tostring(s)  #s 
    //##连接字符串 
    #define stringcat(s1,s2)  s1##s2 
    #define stringcat1(s1,s2)  s1+s2 
    
    int main(void) 
    { 
        printf("%s
    ",tostring(12345)); 
        printf("%d
    ",stringcat(123,456)); 
        printf("%d
    ",stringcat1(123,456)); 
    
        return 0; 
    }
    
    

    结果:

    will@will-laptop:~/ex/10$ ./a.out 
    12345 
    123456 
    579 

    4,条件编译

    #include <stdio.h> 
    
    //#define  DEBUG 
    //定义与未定义产生不同的结果,三个语句就是一套 
    //就是#ifdef  #else  #endif 
    
    int main(void) 
    { 
        #ifdef DEBUG 
               printf("this is a debug version!
    "); 
        #else 
               printf("this is a release version!
    "); 
        #endif 
        #if 1 
               printf("macro condition is true!
    "); 
        #else 
               printf("macro condition is false
    "); 
        #endif 
        return 0; 
    }

    结果:

    will@will-laptop:~/ex/10$ ./a.out 
    this is a release version! 
    macro condition is true! 

    5,gcc和c99宏定义中使用printf和sprintf,

    #include <stdio.h> 
    //"调用"宏时,变参部分参数不能为空 
    //GCC扩展功能 
    #define debug(msg,args...)         
        printf(msg,args) 
    //C99 的用法 
    #define debug1(msg,...)             
        printf(msg,__VA_ARGS__) 
    //"调用"宏时,变参部分参数可以为空 
    //GCC扩展功能 
    #define info(msg,args...)           
        fprintf(stderr,msg,##args) 
     
    
    //C99 的用法 
    #define info1(msg,...)              
        fprintf(stderr,msg,##__VA_ARGS__) 
    
    int main(void) 
    { 
        int a=3,b=5; 
    
        debug("a=%d,b=%d
    ",a,b); 
        debug1("a=%d,b=%d
    ",a,b); 
    
        printf("----------------------------
    "); 
        info("12312312312312312
    "); 
        info1("a=%d,b=%d
    ",a,b); 
    
        return 0; 
    }

    结果:

    will@will-laptop:~/ex/10$ ./a.out 
    a=3,b=5 
    a=3,b=5 
    ---------------------------- 
    12312312312312312 
    a=3,b=5 

    6,条件编译避免头文件包含

    //条件编译的应用 
    //防止头文件包含 
    //如果出现两个头文件都包含同样的一个头文件就需要用到这个东西 
    #ifdef  _HEADER_PROTECT_H_ 
    #define _HEADER_PROTECT_H_ 
    //中间可以放你的文件 
    
    #endif /*_HEADER_PROTECT_H_*/

     7,宏定义实现函数功能

    #include <stdio.h> 
    
    //宏定义的高级应用 
    
    #define swap(a,b) do {                
        typeof(a) __t;                       //typeof可以跟变量和表达式,取变量和表达式值的类型
        __t=a;                              
        a=b;                                  
        b=__t;                           
    }while(0) 
    int main(void) 
    { 
        int a=3,b=5; 
    
        //typedef:gcc 
        typeof(a) flag=1; 
        printf("before swap:a=%d,b=%d
    ",a,b); 
        if(flag) 
            swap(a,b); 
        else 
            printf("no swap
    "); 
        printf("=====================
    "); 
    
        printf("after swap: a=%d,b=%d
    ",a,b); 
        return 0; 
    }

    结果:

    will@will-laptop:~/ex/10$ ./a.out 
    before swap:a=3,b=5 
    ===================== 
    after swap: a=5,b=3 
  • 相关阅读:
    线程
    链表
    String 练习
    23种设计模式
    静态方法
    ASCII码及UTF-8概义
    java语法基础
    二进制、八进制、十进制、十六进制之间转换
    数组概意
    java程序安装 配置环境变量
  • 原文地址:https://www.cnblogs.com/will-boot/p/3355192.html
Copyright © 2011-2022 走看看