C language - self-defined type - enumeration and union (11.3)

Table of contents

mind Mapping:

1. Enumeration

1.1 Definition of enumeration type

1.2 Advantages of enumeration

1.3 Use of enumeration

2. Union (community)

2.1 Definition of union type

2.2 Features of the union

2.3 Calculation of joint size

Write at the end:


mind Mapping:

1. Enumeration

1.1 Definition of enumeration type

example:

enum Day//星期
{
Mon,
Tues,
Wed,
Thur,
Fri,
Sat,
Sun
};

enum Sex//性别
{
MALE,
FEMALE,
SECRET
};

enum Color//颜色
{
RED,
GREEN,
BLUE
};

All of the above are definitions of enumerations.

example:

#include <stdio.h>

enum Sex
{
	MALE,
	FEMALE,
	SECRET
};

int main()
{
	printf("%d\n", MALE);
	printf("%d\n", FEMALE);
	printf("%d\n", SECRET);
	return 0;
}

output:

输出:
0
1
2

After the enumeration is defined, the default value of the members starts from 0 and increments by 1 at a time.

#include <stdio.h>

enum Sex//默认递增1//第一个默认是0
{
	//枚举是可以初始化初始值
	MALE=1,//枚举定义的是常量
	FEMALE=2,
	SECRET=4
};

int main()
{
	enum Sex s;
	printf("%d\n", sizeof(s));//枚举类型的大小是4个字节
	printf("%d ", MALE);
	printf("%d ", FEMALE);
	printf("%d ", SECRET);
	//MALE=2;//不能修改
	return 0;
}

output:

输出:
4
1 2 4

1.2 Advantages of enumeration

1. Increase the readability and maintainability of the code.

2. Compared with the identifier defined by #define, the enumeration has type checking, which is more rigorous.

3. Prevents naming pollution (encapsulation).

4. Easy to debug.

5. Easy to use, multiple constants can be defined at one time.

1.3 Use of enumeration

When I was implementing a static address book,

Case1, 2, 3, 4, 5, 6, 0 in the switch statement are less readable,

We can make it more intuitive with an enum:

Here's the original implementation:

void test()
{
	int input = 0;
    
	//创建通讯录con
	Contact con;

    //初始化通讯录
    InitContact(&con);//分装成函数实现

	do
	{
		menu();//打印菜单
		printf("请选择:>");
		scanf("%d", &input);//选择功能
		switch(input)
		{
		case 1:
            //增加联系人
            AddContact(&con);
			break;
		case 2:
            //删除联系人
            DelContact(&con);
			break;
		case 3:
            //查找联系人
            SearchContact(&con);
			break;
		case 4:
            //修改指定联系人
            ModifyContact(&con);
            break;
		case 5:
            //整理通讯录(按类型排序)
            SortContact(&con);
			break;
		case 6:
            //显示通讯录的信息
            ShowContact(&con);
			break;
		case 0:
            //退出通讯录
			printf("通讯录已退出\n");
			break;
		default:
			printf("选择错误\n");
			break;
		}
	} while (input);
}

We found that, without relying on annotations,

It is difficult for us to distinguish what functions are to be achieved by case0~6.

We can create an enum type:

enum Option
{
	EXIT,//0
	ADD,
	DEL,
	SEARCH,
	MODIFY,
	SORT,
	SHOW
};

void test()
{
	int input = 0;

	//创建通讯录con
	Contact con;

	//初始化通讯录
	InitContact(&con);

	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch(input)
		{
		case ADD:
			AddContact(&con);
			break;
		case DEL:
			DelContact(&con);
			break;
		case SEARCH:
			SearchContact(&con);
			break;
		case MODIFY:
			ModifyContact(&con);
			break;
		case SORT:
			SortContact(&con);
			break;
		case SHOW:
			ShowContact(&con);
			break;
		case EXIT:
			printf("通讯录已退出\n");
			break;
		default:
			printf("选择错误\n");
			break;
		}
	} while (input);
}

In this case, even if we don't rely on annotations,

It can also clearly know what functions should be realized in different cases.

2. Union (community)

2.1 Definition of union type

The members of the union share the same space (so the union is also called the union).

example:

#include <stdio.h>

typedef union UN//这是一个联合体
{
	char c;
	int i;	
}UN;

int main()
{
	UN un;
	printf("%d\n", sizeof(un));
	printf("%p\n", &un);
	printf("%p\n", &(un.c));
	printf("%p\n", &(un.i));
	return 0;
}

output:

输出:
4
006FF7D0
006FF7D0
006FF7D0

It can be found through observation that their addresses are the same.

This is a feature of unions:

2.2 Features of the union

The members of the union share the same memory space, so the size of such a joint variable,

At least the size of the largest member (since the union must be able to hold at least the largest member).

2.3 Calculation of joint size

The size of the union is at least the size of the largest member.

When the maximum member size is not an integer multiple of the maximum alignment, it must be aligned to an integer multiple of the maximum alignment.

example:

#include <stdio.h>

typedef union Un
{
	char c[5];//5个char类型占用5个字节,对齐后占用8个
	int n;//共用内存
}Un;

typedef union Un2
{
	short c[7];//7个short占用14个字节,对齐后占用16个
	int n;//共用内存
}Un2;

int main()
{
	Un un;
	Un2 un2;
	printf("%d\n", sizeof(un));
	printf("%d\n", sizeof(un2));
	return 0;
}

output:

输出:
8
16

Also, we can use unions to compute endianness:

example:

This is how we judged before:

#include <stdio.h>

int main()
{
	int a = 1;//0x00 00 00 01
	//小端存储的方式:
	//低地址------>高地址
	//0x01 00 00 00

	if (*(char*)&a == 1)
	{
		printf("小端\n");
	}
	else
	{
		printf("大端\n");
	}
	return 0;
}

output:

输出:小端

Here's how to do it with unions:

Take advantage of shared memory features:

#include <stdio.h>

typedef union Un
{
	char c;
	int i;
}Un;

int main()
{
	int a = 1;
	Un un;
	un.i = 1;
	if (un.c == 1)
	{
		printf("小端\n");
	}
	else
	{
		printf("大端\n");
	}
	return 0;
}

output:

输出:小端

Write at the end:

The above is the content of this article, thank you for reading.

If you like this article, please like and comment, and write down your opinions.

If you want to learn programming with me, you might as well follow me, we will learn and grow together.

I will output more high-quality content in the future, welcome to watch.

Guess you like

Origin blog.csdn.net/Locky136/article/details/128674189