美文网首页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语法

    1、定义新的类型名 2、定义结构体简称 3、定义数组简称 4、定义指针简称

  • C++ struct

    1、typedef C语言中,使用如下格式 Typedef struct A { Int a; }A_type; ...

  • C语言基础教程之typedef

    C语言 typedef C 语言提供了typedef关键字,您可以使用它来为类型取一个新的名字。下面的实例为单字节...

  • C语言的typedef

    typedef是一种有趣的声明形式:它为一种类型引入新的名字,而不是为变量分配空间。在某些方面,typedef类似...

  • C语言_typedef、union

    @(C语言) [toc] typedef 作用 设置别名,并没有创建新的类型 使用 定义一个二叉树: 现在可以写成...

  • C语言之typedef

    用typedef定义新类型名(给已有类型取别名)在编程中可以用typedef来定义新的类型名来代替已有的类型名格式...

  • 《C语言23—typedef》

    2019年3月25日星期一 多云 (声明:理论知识部分来自菜鸟教程网站!)今日学习内容: 27、C typedef...

  • 初学C语言

    初学C语言——结构的使用 #include #include typedef struct tagdate{ ...

  • ios block详解

    什么是typedef? typedef就是一种替换,与宏不同的是它还可以进行对象的声明。 typedef为C语言的...

  • 位移枚举

    位移枚举 一. OC中常见的三种枚举 C语言枚举 // C语言枚举 typedef enum : NSUInteg...

网友评论

    本文标题:C语言typedef语法

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