Custom type (two): enumeration and union (union)


One, enum constant

As the name implies, the enumeration constant enumerates the possible values ​​one by one, and these possible values ​​have constant properties.

For example:
Monday to Sunday of a week are limited to 7 days, which can be listed in sequence.
There are male and female genders, which can also be listed in turn.
There are 12 months in the month, which can also be listed in sequence.

To record these possible values, arrays are certainly feasible, but the C language provides a more convenient way: enumeration.

1. The definition of enumeration constants

The code is as follows (example):

enum Weekday//星期
{
    
    
	Mon,//常量间用逗号分开
	Tue,
	Wed,
	Thu,
	Fri,
	Sat,
	Sun//注意最后一个常量后没有逗号
};

enum Sex
{
    
    
	Male,
	Female
}

The constants in these enumeration constants have corresponding values. By default, the first constant starts from 0 and increases by 1 in turn. Of course, you can also assign initial values ​​when you define it.

The code is as follows (example):

enum Weekday1
{
    
    
	//不赋初始值,默认从0开始递增1
	Mon,//0
	Tue,//1
	Wed,//2
	Thu,//3
	Fri,//4
	Sat,//5
	Sun//6
};

enum Weekday2
{
    
    
	//给某个常量赋初始值,则从该常量开始递增1
	//该常量之前的不受影响
	Mon,//0
	Tue,//1
	Wed = 10,//10
	Thu,//11
	Fri,//12
	Sat,//13
	Sun//14
};

2. The advantages of enumeration constants

We can use #define to define constants. Why do we have to use enumerations?
The advantages of enumeration:
(1) Increase the readability and maintainability of the code
(2) Compared with the identifier defined by #define, enumeration has type checking, which is more rigorous
(3) Prevents naming pollution (encapsulates constants)
(4) Easy to debug
(5) Easy to use, multiple constants can be defined at one time

2. Consortium (union)

1. Definition of Consortium Type

The union is also a special custom type. The variables defined by this type also contain a series of members. The characteristic is that these members share the same space (so the union is also called a union).

The code is as follows (example):

//联合类型的声明
union Un 
{
    
     
	char c; 
	int i; 
	double d;
}; 
//联合变量的定义
union Un un;

1. Variables in the consortium

Since the variables in the union share the same space, changing one value will affect the other values.

The code is as follows (example):

union Un
{
    
    
	int a;
	int b;
	int c;
};

int main()
{
    
    
	union Un un;
	
	//下面的三个地址均相同
	printf("%d\n", &(un.a));
	printf("%d\n", &(un.b));
	printf("%d\n", &(un.c));
	
	un.a = 1;
	printf("%d\n", un.b);//1
	printf("%d\n", un.c);//1

	un.b = 2;
	printf("%d\n", un.a);//2
	printf("%d\n", un.c);//2
	
	return 0;
}

2. Calculation of the size of the union

The size of the union is at least the size of the largest member.
When the maximum member size is not an integral multiple of the maximum alignment, it must be aligned to an integral multiple of the maximum alignment.

The code is as follows (example):

union Un1 
{
    
     
	char c[5]; 
	int i; 
}; 
union Un2 
{
    
     
	short s[7]; 
	int i; 
}; 
printf("%d\n", sizeof(union Un1)); //8
//c数组占5个字节,对齐数为1;i变量占4个字节,对齐数为4
//联合体大小至少为5个字节,对齐到最大对齐数的整数倍,即为8
printf("%d\n", sizeof(union Un2));//16
//s数组占14个字节,对齐数为2;i变量占4个字节,对齐数为4
//联合体大小至少为14个字节,对齐到最大对齐数的整数倍,即为16

Thanks for reading, please criticize and correct any errors

Guess you like

Origin blog.csdn.net/weixin_51983604/article/details/115359437