【Union and enumeration】

Community

A constructed type of data structure

A union is similar to a structure, and it is also a data structure of a constructed type.
Since it is a constructed type, you need to define the type first, and then use the type to define the variable.
The method of defining a union type is very similar to that of a structure, just change struct to union.
When performing certain algorithms, several different types of variables need to be stored in the same memory unit, and the space used by several variables overlaps with each other. This structure in which several different variables occupy a section of memory together is called a "union" type structure in C language.
All members of the union occupy the same address space.
The size of a union is the size of its largest member in memory.

The characteristics of the community

  1. The same memory segment can be used to store several different types of members, but only one is active at a time.
  2. The effective member of the union variable is the member stored last time, and the value of the original member will be overwritten after a new member is stored.
  3. The address of a union variable and the addresses of its members are the same address.
  4. Initialization of Union Variables union data a={123};Initialize the union as its first member.
#include <stdio.h>

//定义一个共用体
union un{
    
    
	int a;
	int b;
	int c;
};

int main(int argc, char *argv[])
{
    
    
	//定义共用体变量
	union un myun;
	myun.a = 100;
	myun.b = 200;
	myun.c = 300;

	printf("a = %d, b = %d, c = %d\n", myun.a, myun.b, myun.c);

	return 0;
}

Results of the
insert image description here

enumerate

List the values ​​of the variables one by one, and the values ​​of the variables are limited to the range of the listed values.
An enumeration type is also a constructed type.

enum type definition

enum 枚举类型名{
    
    
	枚举值列表;
};

All available values ​​shall be listed in the enumeration value table, also known as enumeration elements.
Enumeration variables can only take the elements listed in the enumeration value.

How to define enumeration variables

enum 枚举类型名 枚举变量名;
  1. The enumeration value is a constant, and it cannot be assigned a value with an assignment statement in the program.
    Example: sun=5; mon=2; sun=mon;Both are wrong.
  2. The enumeration element itself is defined by the system as a numerical value representing a serial number.
    The default is to define the order from 0 as 0, 1, 2...
    For example, in week, the value of mon is 0, the value of tue is 1, ..., and the value of sun is 6.
  3. You can change the default value of the enumeration value: such as
enum week //枚举类型
{
    
    
	mon=3,
	tue,
	wed,
	thu,
	fri=4,
	sat,
	sun
};

mon=3 tue=4, and so on
fri=4and so on
Note: When defining an enumeration type, the enumeration element can be assigned a value with an equal sign to represent the starting number of the element.
In the program, the enumeration element cannot be assigned again, because the enumeration Elements are constants.

#include <stdio.h>

//定义一个枚举类型
enum week
{
    
    
	mon=8, tue, wed, thu=2, fri, sat, sun
};

int main(int argc, char *argv[])
{
    
    
	//定义枚举类型的变量
enum week day = mon;
printf("day = %d\n", day);

day = fri;
printf("day = %d\n", day);

return 0;
}

Results of the
insert image description here

Guess you like

Origin blog.csdn.net/shuting7/article/details/130718755