Detailed Explanation of C Language Enumeration and Union

 

 

This article brings a detailed explanation of enumeration and union related knowledge!

If you think the article is good, I look forward to your one-click three-link. Your encouragement is the source of motivation for my creation. Let us work together, run together, and let us meet at the top! ! !


Table of contents

1. Enumeration

1. What is an enumeration?

2. Definition of enumeration type

3. Advantages of enumeration

4. Use of enumeration

2. Consortium (community)

1. Definition of union type

2. The memory layout and characteristics of the union

Example interview questions:

Reference code and analysis

 3. Calculation of joint size


1. Enumeration

1. What is an enumeration?

Enumeration, as the name implies, is to enumerate one by one
List all possible values
For example in our real life:
There are only 7 days in a week from Monday to Sunday, which can be listed one by one
Gender: male, female, confidential, you can also list them one by one
There are 12 months in the month, and you can also list them one by one
Enumerations can be used here.

2. Definition of enumeration type

for example: 

in:

enum Day , enum Sex , enum Color defined above are all enumeration types
The content in { } is the possible value of the enumeration type, also called enumeration constant 
These possible values ​​are all valid, starting from 0 by default and increasing by 1 in turn. Of course, the initial value can also be assigned when declaring the enumeration type.
For example:
 
 

verify: 

 

You can also assign an initial value when declaring an enumeration type

like:

verify: 

When some enumeration constants are assigned, the size of other values

for example:

verify:

 

3. Advantages of enumeration

We can use #define to define constants, why use enums?
Advantages of enums:
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. Easy to debug
4. Easy to use, you can define multiple constants at a time

4. Use of enumeration

2. Consortium (community)

1.  Definition of joint type

Unions are also a special custom type
Variables defined by this type also contain a series of members, characterized by the fact that these members share the same space (so unions are also called unions)
for example:

 

 

union Un is a union type;

n is a union variable created with this type;

2. The memory layout and characteristics of the union


 

 analyze:

feature:

Because the characteristic of the union is that these members share the same space, but if you want to be able to store these union variables, if you only open up the size of the char type, then the value of a cannot be stored. Therefore, the size of such a joint variable is at least the size of the largest member (because the union must at least have the ability to save the largest member)

Note: Only one can be used at a time for members of the union;


An example of a union feature code: 

Example interview questions:

Determine the size and endian storage of the current computer

Reference code and analysis:

union Un
{
	int a;
	char c;
};
int main()
{
	union Un n;
	n.a = 1;

	if (n.c == 1) 
	{
		printf("小端存储\n");
	}
	else
	{
		printf("大端存储\n");
	}
	return 0;
}

analyze: 

operation result:

 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 number, it must be aligned to an integer multiple of the maximum alignment number
For example:

 analyze:

 verify:

 

end of this chapter~


Guess you like

Origin blog.csdn.net/2301_77509762/article/details/132003574