typedef in C

typedef in C

1. typedef

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

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 的任何时候使用。

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;
}

Output

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 (named student).
在这里,整个示例与在 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;
}

Output

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

References

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

发布了509 篇原创文章 · 获赞 1824 · 访问量 110万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104782728
今日推荐