当前位置:网站首页>C语言之枚举和联合体
C语言之枚举和联合体
2022-07-27 14:54:00 【小白菜00】
目录
枚举
枚举定义
定义:把可能出现的值(有值的常量)一一列举
举例:
- 一周的星期一到星期天可以一一列举
- 性别有男、女可以一一列举
- 月份有12个月可以一一列举
枚举类型的定义
//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;枚举变量的初始化
//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 };//枚举数组
}
//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 };枚举变量的值
枚举变量是有值的(整形类型),并且枚举所有常量没赋初值的情况下,值从0开始由上到下递增的,并且,枚举的每个常量都可以赋初值,对于没赋初值的常量枚举根据已经赋值的常量以最小枚举值为选取方向从上到下逐渐递增(已赋值的枚举常量比后面的没赋值的枚举常量小于1)。
注意:
- 枚举常量必须定义的时候就赋好初值(其是常量不是变量),其他地方不允许赋初值
- 枚举可以写在函数内部进行定义及初始化
#include <stdio.h>
enum Color{
RED,
GREEN,
BLUE
//可以把后面枚举常量考虑成空
};
void main() {
printf("%d\n", RED);//0
printf("%d\n", GREEN);//1
printf("%d\n", BLUE);//2
}#include <stdio.h>
enum Color{
//可以把前面枚举常量考虑成空
RED=5,
GREEN,
BLUE
//可以把后面枚举常量考虑成空
};
void main() {
printf("%d\n", RED);//5
printf("%d\n", GREEN);//6
printf("%d\n", BLUE);//7
}#include <stdio.h>
enum Color{
RED,
//可以把之间的枚举常量考虑成空
GREEN=5,
BLUE
//可以把后面枚举常量考虑成空
};
void main() {
printf("%d\n", RED);//0
printf("%d\n", GREEN);//5
printf("%d\n", BLUE);//6
}#include <stdio.h>
enum Color{
//可以把前面的枚举常量考虑为空
RED=7,
GREEN,
BLUE=0,
YELLOW,
PINK
//可以把后面的枚举常量考虑为空
};
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
}总结:对于未定义枚举值的大小,总是以最小枚举值为选取方向(被赋值的枚举变量仅仅作为一个参考值)。
和#define定义常量相比枚举的优点

枚举类型的大小
#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
}联合体(共用体)
联合体定义
前言:VS编译器存储数据采用的是小端字节序
联合也是一种特殊的自定义类型,这种类型定义的变量也包含一系列的成员,特征是这些成员共用一块空间(所以联合也叫共用体)
注意:联合体的定义与初始化以及成员的调用……与结构体如出一辙
#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
//可以看出联合体的成员变量的地址与联合体的地址是一样的
}联合体的特点
- 联合体的成员是共用一块内存空间的,这样一个联合变量的大小,至少是最大成员的大小(因为联合至少有能力保存最大的那个成员)
- 共用体实际上是C语言提供的一种覆盖技术
- 同一个内存段可以用来存放几种不同类型的成员,但在每一瞬只能存放其中一种。
- 共用体变量中起作用的成员变量是最后一次存放的成员,在存入一个新成员后,原有成员就会失去作用
举例:
#include <stdio.h>
union Un
{
char c;
int i;
};
void main() {
//这样赋值会直接给2个成员变量一起赋值
union Un u = {10};
printf("%d\n",u.c);//10
printf("%d\n",u.i);//10
//分别赋值
u.i = 1000;
printf("%d\n", u.c);//-24——整形提升加截断
printf("%d\n", u.i);//10
u.c = 15;
printf("%d\n", u.c);//15
printf("%d\n", u.i);//783
//由此观之,在共用体中,后存放的成员变量会影响之前存放的成员变量
}注意:
共用体类型可以出现在结构体类型定义中,也可以定义共用体数组,反之,结构体也可以出现在共用体类型定义中,数组也可以作为共用体成员
共用体的使用: 比如一个人在生活中会扮演着多个角色,这些角色通常会不停的切换。
联合体大小的计算
- 联合体的大小至少是最大成员的大小
- 当最大成员的大小不是最大对齐数的整数倍的时候,就要对齐到最大对齐数的整数倍
#include <stdio.h>
union Un
{
char a[5];//5
int i;//4
};
void main() {
union Un u;
printf("%d\n", sizeof(u));//8
//由此观之,所有成员与共用体的地址偏移量为0,a数组占5字节,i变量占4字节。观之最大对齐数为4,5不是最大对齐数4的整数倍,因此扩容到8
}边栏推荐
- 获取当前时间的前N天和前后天的数组列表循环遍历每一天
- 补充—整数规划例题
- 字符流读取文件
- Interpretation of C basic syntax: summarize some commonly used but easily confused functions (i++ and ++i) in the program (bit field)
- Handling of multiple parts with duplicate names and missing parts when importing SOLIDWORK assemblies into Adams
- android中的图片三级缓存
- 最大子段和 Go 四种的四种求解
- 【论文阅读】Transformer with Transfer CNN for Remote-Sensing-ImageObject Detection
- Duplicate numbers in array
- MySQL high version report SQL_ mode=only_ full_ group_ By exception
猜你喜欢
![Jerry's built-in touch parameters for modification [chapter]](/img/6b/38c3ad28a7256e5e41bb444d0993db.png)
Jerry's built-in touch parameters for modification [chapter]

jsp-El表达式,JSTL标签

Kubesphere multi node installation error

Supplement - example of integer programming

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

Product axure9 English version, using repeater repeater to realize drop-down multi selection box

自然排序:comparable接口,定制排序:compartor接口的区别

File类字节输入、输出流

KMEANS 实现

Rotate string left
随机推荐
As changes the background theme and background picture
MySQL—连表查询
Implementation of filler creator material editing tool
Matplotlib drawing error: "! Latex error: file `type1cm.sty 'not found." solution
Product axure9 English version, using repeater repeater to realize drop-down multi selection box
雪花ID(Go 实现)
android中的图片三级缓存
[paper reading] a CNN transformer hybrid approach for cropclassification using multitemporalmultisensor images
Servlet用Cookie实现用户上次登录时间
Jerry's in ear detection function [chapter]
Circular statements and arrays
Kmeans implementation
Snowflake ID (go Implementation)
Two methods of generating excel table with PHP
The difference between MVC and MVP and MVVM
Pointer summary
最大子段和 Go 四种的四种求解
什么是jsp?
*List reversal
In addition to "adding machines", in fact, your micro service can be optimized like this