C 语言中的 typedef

本文详细介绍了C语言中typedef关键字的用法,包括如何为基本类型和用户定义的类型如结构体分配新名称,以简化代码书写。通过实例展示了typedef在不同场景下的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C 语言中的 typedef

1. typedef in C

typedef keyword is used to assign a new name to a type. This is used just to prevent us from writing more.
typedef keyword 用于为类型分配新名称,这只是为了防止我们编写更多内容。

For example, if we want to declare some variables of type unsigned int, we have to write unsigned int in a program and it can be quite hectic for some of us. So, we can assign a new name of our choice for unsigned int using typedef which can be used anytime we want to use unsigned int in a program.
如果我们要声明一些类型为 unsigned int 的变量,则必须在程序中编写 unsigned int。我们可以使用 typedefunsigned int 分配一个新选择的名称,该名称可以在我们想在程序中使用 unsigned int 的任何时候使用。

hectic ['hektɪk]:n. 肺病热,肺病热患者,潮红 adj. 忙碌的,繁忙的
typedef current_name new_name;
typedef unsigned int uint;
uint i, j;

Now, we can write uint in the whole program instead of unsigned int. The above code is the same as writing:
我们可以在整个程序中编写 uint,而不是 unsigned int。上面的代码与下面编写的相同:

unsigned int i, j;

Let’s see an example.

//============================================================================
// Name        : typedef
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <stdio.h>

int main()
{
	typedef unsigned int ui;
	ui i = 5, j = 8;

	printf("i = %d\n", i);
	printf("j = %d\n", j);

	return 0;
}

Output

i = 5
j = 8

Thus, we can assign a new name to any data type.
因此,我们可以为任何数据类型分配一个新名称。

Similarly, we can also use typedef to assign a new name to structure which is a user-defined datatype as follows:
类似地,我们也可以使用 typedef 为结构体分配一个新名称,该结构是用户定义的数据类型,如下所示:

//============================================================================
// Name        : typedef - struct
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

typedef struct structure_name
{
	data_type member_name1;
	data_type member_name2;
	data_type member_name3;
	data_type member_name4;
} type_name;

struct structure_name
{
	data_type member_name1;
	data_type member_name2;
	data_type member_name3;
	data_type member_name4;
} object_names;

注意上面定义中的 structure_nametype_name
注意上面定义中的 structure_nameobject_names

Now, while declaring variables of this structure type, we can write type_name in place of struct structure_name in the whole program.
在声明这种结构体类型的变量时,我们可以在整个程序中写成 type_name 代替 structure_name

Let’s take the example of structure named student which we saw in the Structure topic.
让我们以在结构体主题中看到的名为 student 的结构体为例。

//============================================================================
// Name        : typedef
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <stdio.h>
#include <string.h>

typedef struct student
{
	int roll_no;
	char name[30];
	int phone_number;
} st;

int main()
{
	st p1, p2, p3;

	p1.roll_no = 1;
	strcpy(p1.name, "Brown");
	p1.phone_number = 123443;

	p2.roll_no = 2;
	strcpy(p2.name, "Sam");
	p2.phone_number = 1234567822;

	p3.roll_no = 3;
	strcpy(p3.name, "Addy");
	p3.phone_number = 1234567844;

	printf("First Student\n");
	printf("roll_no : %d\n", p1.roll_no);
	printf("name : %s\n", p1.name);
	printf("phone_number : %d\n", p1.phone_number);

	printf("Second Student\n");
	printf("roll_no : %d\n", p2.roll_no);
	printf("name : %s\n", p2.name);
	printf("phone_number : %d\n", p2.phone_number);

	printf("Third Student\n");
	printf("roll_no : %d\n", p3.roll_no);
	printf("name : %s\n", p3.name);
	printf("phone_number : %d\n", p3.phone_number);

	return 0;
}

First Student
roll_no : 1
name : Brown
phone_number : 123443
Second Student
roll_no : 2
name : Sam
phone_number : 1234567822
Third Student
roll_no : 3
name : Addy
phone_number : 1234567844

Here, the whole example is the same as we did in Structure, the only difference is that we wrote st in place of struct student i.e. we used the new type (named st) to declare the variables of this structure type.
在这里,整个示例与在 Structure 中所做的相同,唯一的区别是我们写了 st 来代替 struct student,即我们使用了新类型 (名为 st) 来声明此结构体类型的变量。

We can also use typedef with union. For this, everything will be same as that of structure with the keyword union in place of struct.
我们也可以结合使用 typedefunion。所有内容都将与关键字 union 代替 struct 的结构相同。

//============================================================================
// Name        : typedef
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <stdio.h>
#include <string.h>

typedef union student
{
	int roll_no;
	int phone_number;
	char name[30];
} st;

int main()
{
	st p1, p2, p3;

	p1.roll_no = 1;
	p1.phone_number = 123443;
	strcpy(p1.name, "Brown");

	p2.roll_no = 2;
	p2.phone_number = 1234567822;
	strcpy(p2.name, "Sam");

	p3.roll_no = 3;
	p3.phone_number = 1234567844;
	strcpy(p3.name, "Addy");

	printf("First Student\n");
	printf("roll_no : %d\n", p1.roll_no);
	printf("phone_number : %d\n", p1.phone_number);
	printf("name : %s\n", p1.name);

	printf("Second Student\n");
	printf("roll_no : %d\n", p2.roll_no);
	printf("phone_number : %d\n", p2.phone_number);
	printf("name : %s\n", p2.name);

	printf("Third Student\n");
	printf("roll_no : %d\n", p3.roll_no);
	printf("phone_number : %d\n", p3.phone_number);
	printf("name : %s\n", p3.name);

	return 0;
}

First Student
roll_no : 2003792450
phone_number : 2003792450
name : Brown
Second Student
roll_no : 7168339
phone_number : 7168339
name : Sam
Third Student
roll_no : 2036622401
phone_number : 2036622401
name : Addy

2. typedef struct in C

The C language contains the typedef keyword to allow users to provide alternative names for the primitive (e.g.,​ int) and user-defined​ (e.g struct) data types.

Remember, this keyword adds a new name for some existing data type but does not create a new type.

primitive ['prɪmətɪv]:adj. 原始的,远古的,发展早期的,简陋的 n. 文艺复兴前的艺术家,原始派画家

2.1 example

/*
============================================================================
Name        : foreverstrong.cpp
Author      : Yongqiang Cheng
Version     : Version 1.0.0
Copyright   : Copyright (c) 2020 Yongqiang Cheng
Description : Hello World in C++, Ansi-style
============================================================================
*/

#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <stdio.h>

struct Point {
	int x;
	int y;
};

int main() {
	struct Point p1;
	p1.x = 3;
	p1.y = 7;
	printf("%d\n", p1.x);
	printf("%d\n", p1.y);

	return 0;
}

3
7
请按任意键继续. . .

2.2 example

/*
============================================================================
Name        : foreverstrong.cpp
Author      : Yongqiang Cheng
Version     : Version 1.0.0
Copyright   : Copyright (c) 2020 Yongqiang Cheng
Description : Hello World in C++, Ansi-style
============================================================================
*/

#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <stdio.h>

struct Point {
	int x;
	int y;
};

typedef struct Point POINT;

int main() {
	POINT p1;
	p1.x = 3;
	p1.y = 7;
	printf("%d\n", p1.x);
	printf("%d\n", p1.y);

	return 0;
}

3
7
请按任意键继续. . .

2.3 example

/*
============================================================================
Name        : foreverstrong.cpp
Author      : Yongqiang Cheng
Version     : Version 1.0.0
Copyright   : Copyright (c) 2020 Yongqiang Cheng
Description : Hello World in C++, Ansi-style
============================================================================
*/

#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <stdio.h>

typedef struct Point {
	int x;
	int y;
} POINT;

int main() {
	POINT p1;
	p1.x = 3;
	p1.y = 7;
	printf("%d \n", p1.x);
	printf("%d \n", p1.y);

	return 0;
}

References

https://www.codesdope.com/c-typedef/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值