当前位置:网站首页>Custom Types - Enums, Unions
Custom Types - Enums, Unions
2022-08-01 09:35:00 【Wang Honghua x】
目录
①Values in enumeration types are constants
一、枚举
1、定义
枚举,is to list all the possibilities.
such as a week7天,We can customize an enumeration type Day,To Monday to Sunday enumerated type.
2、Features and Benefits
①Values in enumeration types are constants
②增加代码可读性
The following is a simple address book code,The program needs to perform different functions according to the user's choice.
#include <stdio.h>
void menu()
{
printf("-----------------------------");
printf("-----1.add 2.delet -----");
printf("-----3.search 4.modify-----");
printf("-----5.show 6.empty -----");
printf("-----7.sort 0.exit -----");
printf("-----------------------------");
}
int main()
{
int input = 0;
do
{
menu();
printf("请选择:>");
switch (input)
{
case 1:
Add();
break;
case 2:
Delet();
break;
case 3:
Search();
break;
case 4:
Modify();
break;
case 5:
Show();
break;
case 6:
Empty();
break;
case 7:
Sort();
break;
case 0:
printf("程序退出!\n");
break;
default:
printf("输入错误!\n");
break;
}
} while (input);
return 0;
}
可以发现在switch,caseStatements in the code a little abstract,For example when we writecase 7: 的时候,We may have forgotten7代表什么,need to flip up,而这个代码,可以优化.
#include <stdio.h>
void menu()
{
printf("-----------------------------");
printf("-----1.add 2.delet -----");
printf("-----3.search 4.modify-----");
printf("-----5.show 6.empty -----");
printf("-----7.sort 0.exit -----");
printf("-----------------------------");
}
enum MENU
{
EXIT,
ADD,
DELET,
SEARCH,
MODIFY,
SHOW,
EMPTY,
SORT
};
int main()
{
int input = 0;
do
{
menu();
printf("请选择:>");
switch (input)
{
case ADD:
Add();
break;
case DELET:
Delet();
break;
case SEARCH:
Search();
break;
case MODIFY:
Modify();
break;
case SHOW:
Show();
break;
case EMPTY:
Empty();
break;
case SORT:
Sort();
break;
case EXIT:
printf("程序退出!\n");
break;
default:
printf("输入错误!\n");
break;
}
} while (input);
return 0;
}
这样在case时,Very clear know what to do next.
③增加类型检查,更加严谨
This feature is mainly reflected inC++中,如下:
在C++语言中,Enumeration types can only be assigned their own defined values,and cannot assign other types of values,更严谨 .
二、联合(共用体)
1、定义
Variables defined by union types are the same as structs,Can be of many different types,区别在于,Member variables of union types share the same space,So share body joint is also called.Below is an example of a complex:
2、Features and Benefits
①共用同一块空间
可以看到,它们的地址都是一样的,同样,If you change the value of one of the variables,Another variable may also change.
So union type variables are used when,Usually only one of the values is used,or in some special cases.
②union type size
The article has already mentioned,Member variables of union types share the same space,So is the size of the union type the size of the largest member??请看下面一段代码:
#include <stdio.h>
typedef union Un
{
char arr[5];
int i;
}Un;
int main()
{
printf("%d", sizeof(Un));
return 0;
}
是不是char arr[5]占5个字节,int i 占4个字节,If it is bigger, it is5呢?
但运行结果却是 8,这是因为,Unions are the same as structs,memory alignment,char arr[5] 的对齐数是1,int i 的对齐数是4,所以UnThe size of the footprint is must4的倍数,也就是8个字节.
边栏推荐
- 会议OA(待开会议&所有会议)
- mysql在cmd的登录及数据库与表的基本操作
- TiDB的真实数据库数据是存在kv和还是pd上?
- How to ensure the consistency of database and cache data?
- 网络个各种协议
- AC与瘦AP的WLAN组网实验
- redis
- sql server, FULL mode, dbcc shrinkfile(2,1) can not shrink the transaction log, or the original size, why?
- ASP.NET Core 6框架揭秘实例演示[30]:利用路由开发REST API
- notes....
猜你喜欢
Detailed explanation of JVM runtime data area and JMM memory model
opencv创建窗口—cv.namedWindow()
Idea common plugins
Leetcode - 6135: the longest part of the figure
How programmers learn open source projects, this article tells you
自定义IP在PCIE中使用
消息队列面试题(2022最新整理)
Idea 常用插件
Redis middleware (from building to refuse pit)
leetcode-6132: Make all elements in array equal to zero
随机推荐
js中如何实现深拷贝?
SQL Server database schema and objects related knowledge notes
Classify GBase 8 s lock
Intensive reading of ACmix papers, and analysis of its model structure
用OpenCV的边缘检测
ASP.NET Core 6框架揭秘实例演示[30]:利用路由开发REST API
消息队列面试题(2022最新整理)
STM32个人笔记-嵌入式C语言优化
network basic learning
mysql login in cmd and basic operations of database and table
leetcode-6132: Make all elements in array equal to zero
ogg同步oracle到mysql,字段里面可能有需要转义的字符,怎么配置转义?
leetcode-6134:找到离给定两个节点最近的节点
PerViT: 神经网络也能像人类利用外围视觉一样观察图像!
SkiaSharp 之 WPF 自绘 五环弹动球(案例版)
How does UXDB return the number of records for all tables in the current database?
Static Pod, Pod Creation Process, Container Resource Limits
对于小应用来讲,使用哪款数据库比较好?
TiDB的真实数据库数据是存在kv和还是pd上?
Change Servlet project to SSM project