美文网首页C
C语言typedef语法

C语言typedef语法

作者: taobao | 来源:发表于2019-10-17 15:22 被阅读0次

    1、定义新的类型名

    #typedef int bool;
    define true 1
    define false 0
    
    bool a = true
    bool b = false
    

    2、定义结构体简称

    struct Node {
      int x;
    };
    struct Node n = {1};
    
    typedef struct Node {
      int x;
    } myNode;
    myNode n = {1};
    

    3、定义数组简称

    #typedef int INT_ARR_100 [100];
    #typedef char CHAR_ARR_100 [100];
    
    INT_ARR_100 a = {1, 2, 3, 4};
    printf("%d %d %d %d\n", a[0], a[1], a[2], a[3]);
    CHAR_ARR_100 b = "test7890";
    printf("%s\n", b);
    

    4、定义指针简称

    #typedef char * PCHAR;
    
    PCHAR a = "test666";
    printf("%s\n", a);
    
    #include<stdio.h>
      
    //定义新的类型名
    typedef int bool;
    
    //为自定义数据类型(结构体、共同体和枚举类型)定义简洁的名称
    typedef struct Point {
            int x;
    } myPoint;
    
    //为数组定义简洁名称
    typedef int INT_ARR_100 [100];
    typedef char CHAR_ARR_100 [100];
    
    //为指针定义简洁名称
    typedef char * PCHAR;
    
    int main(int argc, char *argv[])
    {
            bool true=1,false=0;
            printf("%d %d\n", true, false);
    
            //使用全称
            struct Point p1;
            p1.x = 1;
            printf("%d\n", p1.x);
    
            //使用简称
            myPoint p2;
            p2.x = 2;
            printf("%d\n", p2.x);
    
            INT_ARR_100 arr = {1, 2, 3, 4};
            printf("%d %d %d %d\n", arr[0], arr[1], arr[2], arr[3]);
            CHAR_ARR_100 str = "abcdefg";
            printf("%s\n", str);
    
            PCHAR a = "test char*";
            printf("%s\n", a);
    
            return 0;
    }
    

    相关文章

      网友评论

        本文标题:C语言typedef语法

        本文链接:https://www.haomeiwen.com/subject/agnmmctx.html