当前位置:网站首页>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个字节.
边栏推荐
猜你喜欢

Explain / Desc execution plan analysis

leetcode-6133:分组的最大数量

leetcode-6135:图中的最长环

朴素贝叶斯--学习笔记--基本原理及代码实现

What's up with VS "Cannot find or open PDB file"?How to solve

leetcode-6134: Find the closest node to the given two nodes

企业微信群:机器人定时提醒功能数据库配置化

自定义IP在PCIE中使用

various network protocols

Meeting OA (Upcoming Meetings & All Meetings)
随机推荐
Shell:条件测试操作
Parsing MySQL Databases: "SQL Optimization" vs. "Index Optimization"
rpm和yum
SkiaSharp's WPF self-painted five-ring bouncing ball (case version)
MySQL 必现之死锁
redis
灵魂发问:MySQL是如何解决幻读的?
WLAN networking experiment of AC and thin AP
Visualization - Superset installation and deployment
网络基础学习
报告:想学AI的学生数量已涨200%,老师都不够用了
Detailed explanation of JVM runtime data area and JMM memory model
杨辉三角(c语言实现)
消息队列面试题(2022最新整理)
Explain / Desc execution plan analysis
HoloView -- Tabular Datasets
【无标题】
[Interview: Concurrency 39: Multithreading: Thread Pool] ThreadPoolExecutor Class - Submit, Stop
将Servlet项目改为SSM项目
笔记。。。。