当前位置:网站首页>Enumeration and union of C language
Enumeration and union of C language
2022-07-27 16:55:00 【Cabbage 00】
Catalog
Definition of enumeration type
Initialization of enumeration variables
Enumerate the values of variables
and #define Advantages of defining constants over enumerations
Characteristics of the consortium
Calculation of the size of the consortium
enumeration
Enumeration Definition
Definition : Put the possible values ( Constant with value ) List one by one
give an example :
- Monday to Sunday of the week can be listed one by one
- Gender has male 、 Women can list them one by one
- Month has 12 Months can be listed one by one
Definition of enumeration type
//1
enum Color {
RED,
GREEN,
BLUE
};
void main() {
enum Color a, b;
}
//2
enum {
RED,
GREEN,
BLUE
}a;
//3
enum Color{
RED,
GREEN,
BLUE
}a,b;Initialization of enumeration variables
//1
enum Color{
RED=7,
GREEN,
BLUE=2,
};
void main() {
enum Color c=RED,e=BLUE;
enum Color d; d = GREEN;
enum Color f[3]= { RED,GREEN,BLUE };// Enumerating arrays
}
//2
enum Color{
RED = 7,
GREEN,
BLUE = 2,
}a = RED, b;
void main() {
b = BLUE;
enum Color c = GREEN;
}
//3
enum {
RED = 7,
GREEN,
BLUE = 2,
}a = RED, b = BLUE, f[3] = { RED,GREEN,BLUE };Enumerate the values of variables
Enumeration variables have values ( Type of plastic surgery ), And enumerate all constants without initial values , Value from 0 Starting from top to bottom , also , Each constant of enumeration can be assigned an initial value , For constants without initial values, enumeration gradually increases from top to bottom according to the assigned constants with the minimum enumeration value as the selection direction ( The assigned enumeration constant is less than 1).
Be careful :
- The initial value is assigned when the enumeration constant must be defined ( It is a constant, not a variable ), Initial values are not allowed in other places
- Enumerations can be defined and initialized inside functions
#include <stdio.h>
enum Color{
RED,
GREEN,
BLUE
// You can consider the following enumeration constants as null
};
void main() {
printf("%d\n", RED);//0
printf("%d\n", GREEN);//1
printf("%d\n", BLUE);//2
}#include <stdio.h>
enum Color{
// You can consider the above enumerated constants as null
RED=5,
GREEN,
BLUE
// You can consider the following enumeration constants as null
};
void main() {
printf("%d\n", RED);//5
printf("%d\n", GREEN);//6
printf("%d\n", BLUE);//7
}#include <stdio.h>
enum Color{
RED,
// You can consider the enumeration constants between as empty
GREEN=5,
BLUE
// You can consider the following enumeration constants as null
};
void main() {
printf("%d\n", RED);//0
printf("%d\n", GREEN);//5
printf("%d\n", BLUE);//6
}#include <stdio.h>
enum Color{
// You can consider the previous enumeration constant to be empty
RED=7,
GREEN,
BLUE=0,
YELLOW,
PINK
// You can consider the following enumeration constants to be empty
};
void main() {
printf("%d\n", RED);//7
printf("%d\n", GREEN);//8
printf("%d\n", BLUE);//0
printf("%d\n", YELLOW);//1
printf("%d\n", PINK);//2
}summary : For the size of undefined enumeration values , Always take the minimum enumeration value as the selection direction ( The enumeration variable assigned is only used as a reference value ).
and #define Advantages of defining constants over enumerations

Enumeration type size
#include <stdio.h>
enum Color{
RED = 7,
GREEN,
BLUE = 2,
}d=BLUE,f[3] = { RED,GREEN,BLUE };
void main() {
printf("%d\n", sizeof(enum Color));//4
printf("%d\n", sizeof(d));//4
printf("%d\n", sizeof(f));//12
}Consortium ( Shared body )
Consortium definition
Preface :VS The compiler stores data in small endian byte order
Federation is also a special custom type , Variables defined by this type also contain a series of members , The feature is that these members share a space ( So union is also called community )
Be careful : The definition and initialization of the consortium and the invocation of members …… Just like the structure
#include <stdio.h>
union Un
{
char c;
int i;
}u;
void main() {
printf("%d\n", sizeof(u));//4
printf("%d\n", sizeof(union Un));//4
printf("%p\n", &u);//0073A144
printf("%p\n", &(u.c));//0073A144
printf("%p\n", &(u.i));//0073A144
// It can be seen that the address of the member variable of the consortium is the same as that of the consortium
}Characteristics of the consortium
- The members of the consortium share a memory space , The size of such a joint variable , At least the size of the largest member ( Because the Union has at least the ability to preserve the largest member )
- The common body is actually C Language provides a coverage technology
- The same memory segment can be used to hold several different types of members , But only one of them can be stored in every moment .
- The active member variable in the common body variable is the last stored member , After depositing a new member , The original members will lose their function
give an example :
#include <stdio.h>
union Un
{
char c;
int i;
};
void main() {
// This assignment will directly give 2 Member variables are assigned together
union Un u = {10};
printf("%d\n",u.c);//10
printf("%d\n",u.i);//10
// Assign values respectively
u.i = 1000;
printf("%d\n", u.c);//-24—— Shaping, lifting and truncation
printf("%d\n", u.i);//10
u.c = 15;
printf("%d\n", u.c);//15
printf("%d\n", u.i);//783
// From this point of view , In the common body , The member variables stored later will affect the member variables stored before
}Be careful :
The common body type can appear in the structure type definition , You can also define a common body array , conversely , A structure can also appear in a common body type definition , Arrays can also be members of a community
Use of community : For example, a person will play multiple roles in life , These roles usually switch constantly .
Calculation of the size of the consortium
- The size of the consortium is at least the size of the largest member
- When the size of the largest member is not an integer multiple of the maximum alignment number , It's about aligning to an integer multiple of the maximum number of alignments
#include <stdio.h>
union Un
{
char a[5];//5
int i;//4
};
void main() {
union Un u;
printf("%d\n", sizeof(u));//8
// From this point of view , The address offset between all members and the community is 0,a Array occupation 5 byte ,i Variables of 4 byte . The maximum alignment number is 4,5 Not the maximum number of alignments 4 Integer multiple , So expand the capacity to 8
}边栏推荐
- Apache
- Exe program encryption lock
- Jerry's maximum volume prompt sound cannot be broadcast [article]
- 201403-1
- Collection! 0 basic open source data visualization platform flyfish large screen development guide
- ROS - error in running.Py file in the project
- 牛客题目——二叉搜索树与双向链表
- Cryptography series: certificate format representation of PKI X.509
- Scala branch control (single branch, double branch, multi branch), return value of branch judgment
- Operators of C language
猜你喜欢
随机推荐
Servlet用Cookie实现用户上次登录时间
C language output string in reverse order
(二)动态卷积之Dynamic Convolution
Insert pictures in word to maintain high DPI method
JDBC程序实现完整步骤
牛客题目——链表的奇偶重排、输出二叉树的右视图、括号生成、字符流中第一个不重复的字符
Opencv (I) -- basic knowledge of image
UML diagram introduction
Gurobi——GRBLinExpr
LNMP环境--部署wordpress
Casadi -- detailed explanation of data types and introduction to basic operations
(2) Dynamic convolution of dynamic convolution
C语言之字符函数和字符串函数及内存函数
mvc和mvp和mvvm的区别
Servlet Chinese garbled setcontenttype setting is invalid
Random number formula random
Is low code the future of development? On low code platform
C语言之结构体及位段
密码学系列之:PKI的证书格式表示X.509
牛客题目——二叉搜索树与双向链表








