[C language advanced study notes] 4. Custom type (2) (enumeration + union)


enumerate

What is an enumeration?

As the name suggests, an enumeration is an enumeration. Enumerating all possible values ​​is an enumeration.

For example, in our real life:
①The possible values ​​of a week, from Monday to Sunday, a total of 7 possible values
​​②The value of gender, male, female, confidential, a total of 3 possible values ​​(of course you can also Add another possible value, such as unknown, but the possible values ​​after adding can still be listed one by one, a total of 4 possibilities)
③The possible values ​​of the month, from January to December, a total of 12 possible values
​​④ Possible values ​​of colors, such as red, orange, yellow, green, blue, blue, purple


How to define an enumeration type?

enum Day//星期
{
    
    
	Mon,
	Tues,
	Wed,
	Thur,
	Fri,
	Sat,
	sun
};
enum sex//性别
{
    
    
	MALE,
	FEMALE,
	SECRET
};
enum color//颜色--三原色 rgb
{
    
    
	RED,
	GREEN,
	BLUE
};

The enum Day, enum sex, and enum color defined above are all enumeration types.
These possible values ​​all have values. The default starts from 0 and increments by 1 at a time. Of course, initial values ​​can also be assigned when they are defined. These values ​​are called enumeration constants.
E.g:

enum color//颜色--三原色 rgb
{
    
    
	RED = 1,
	GREEN = 2,
	BLUE = 3
};

At this time, the constant values ​​of the enumeration type are 1, 2, and 3.


Advantages of enumeration

Why use enums?

We can use #define to define constants, why do we have to use enumeration?
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 and is more rigorous .
3. Prevents naming pollution (encapsulation)
4. Easy to debug
5. Easy to use, you can define multiple constants at a time


use of enumeration

enum color//颜色
{
    
    
	RED = 1,
	GREEN = 2,
	BLUE = 4
};
enum color c1r = GREEN;//只能拿枚举常量给枚举变量赋值,才不会出现类型的差异。
clr = 5;// ok ? ?
//枚举常量和常量值是有区别的,将常量直接赋值给枚举常量类型,编译器会报错或警告

Extension: If a value of an enumeration type is assigned a custom value in the middle, the value in front of it is still incremented from 0, and the value behind it is incremented according to the custom value.
insert image description here
An enumeration is a type that can be used to define variables -- enumeration variables, but whose members are constant values ​​-- enumeration constants


Union type

Definition of Community Type

A union is also a special self-defined type. The variable defined by this type also contains a series of members, which are characterized by the fact that these members share the same space (some books also call unions as unions).

For example:

#include<stdio.h>
	//共用体类型的声明
	union Un
{
    
    
	char c;
	int i;
};
int main()
{
    
    
	union Un u;
	printf("%d\n", sizeof(u));
	printf("%d\n", sizeof(u.c));
	printf("%d\n", sizeof(u.i));

	printf("%p\n", &u);
	printf("%p\n", &(u.c));
	printf("%p\n", &(u.i));
	return 0;
}

insert image description here
Memory Analysis:
insert image description here


Features of the Commonwealth

The members of the union share the same memory space, and the size of such a union variable is at least the size of the largest member (because the union must at least be able to save the largest member).


Community size calculation

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

Example:

#include<stdio.h>
union u1
{
    
    
	char ch[5];
	int i;
};
union u2
{
    
    
	short s[7];
	int i;
};
int main()
{
    
    
	printf("%d\n", sizeof(union u1));
	printf("%d\n", sizeof(union u2));
	return 0;
}

analyze:

The array ch in u1 has 5 elements and the size is 5 bytes, so the size of u1 is 5, but because 5 is not a multiple of the maximum alignment number 4 (int), the size after aligning to the maximum alignment number 4 is 8.
The array s in u2 has 7 elements, and the size is 7 * 2 = 14 bytes. 14 is not a multiple of the maximum alignment number of 4, and the size is 16 after being aligned to a multiple of 4.

Results show:
insert image description here


Examples of application scenarios of the Commonwealth

Using the characteristics of the union, we can use it to determine the endian byte order of the computer

#include<stdio.h>
union u1
{
    
    
	char ch;
	int i;
};

int main()
{
    
    
	union u1 a;
	a.i = 1;
	if (a.ch == 1)
		printf("小端字节序\n");
	else
		printf("大端字节序\n");
	return 0;
}

understand:
insert image description here

Summary:
There is memory alignment in structures and unions. There is no memory alignment
in enumeration and bit segments.

Guess you like

Origin blog.csdn.net/QIYICat/article/details/118631368