当前位置:网站首页>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
- Array analog linked list
- Ten thousand words analysis ribbon core components and operation principle
- Pointer elementary of C language
- Measured: the performance of cloud RDS MySQL is 1.6 times that of self built
- 除了「加机器」,其实你的微服务还能这样优化
- 清晰的认识Torchvision(思维导图版)
- After the cubemx is reconfigured, the generated code cannot be opened successfully with IAR
- D3.js create a cool arc
- Rotate the whole model in ADAMS
猜你喜欢

为媒体资产构建一个云原生的文件系统

【论文阅读】Single- and Cross-Modality Near Duplicate Image PairsDetection via Spatial Transformer Compar

Snowflake ID (go Implementation)

The 21st - -- the 30th time

2021 national vocational college skills competition (secondary vocational group) network security competition questions (9) ideas

【论文阅读】A CNN-Transformer Hybrid Approach for CropClassification Using MultitemporalMultisensor Images

codis集群部署

Fast Planner - detailed explanation of kinetic astar

清晰的认识Torchvision(思维导图版)

CODIS cluster deployment
随机推荐
C语言之字符函数和字符串函数及内存函数
Interpretation of C basic syntax: summarize some commonly used but easily confused functions (i++ and ++i) in the program (bit field)
string数字类型转换为千分位
万字剖析Ribbon核心组件以及运行原理
Stylelint check error: unexpected missing generic font family font family no missing generic family keyword
If you don't want to step on those holes in SaaS, you must first understand the "SaaS architecture"
Layoff quarrel, musk: I'm too hard; Mercilessly open source a public opinion acquisition project; Feature engineering is as simple as parameter adjustment?! Nerf boss shouted that he couldn't move; Cu
MySQL—连表查询
excel skill
After the cubemx is reconfigured, the generated code cannot be opened successfully with IAR
为媒体资产构建一个云原生的文件系统
Kubesphere multi node installation error
牛客题目——判断是不是完全二叉树、平衡二叉树
【论文阅读】A CNN-Transformer Hybrid Approach for CropClassification Using MultitemporalMultisensor Images
jsp-El表达式,JSTL标签
Collection! 0 basic open source data visualization platform flyfish large screen development guide
Insert pictures in word to maintain high DPI method
Jerry's maximum volume prompt sound cannot be broadcast [article]
Duplicate names in molecular class methods
C语言之结构体及位段