当前位置:网站首页>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个字节.
边栏推荐
猜你喜欢
ACmix 论文精读,并解析其模型结构
Visualization - Superset installation and deployment
How to ensure the consistency of database and cache data?
The soul asks: How does MySQL solve phantom reads?
报告:想学AI的学生数量已涨200%,老师都不够用了
【数据集】各类绝缘子、鸟巢及防震锤数据集汇总
leetcode-6134:找到离给定两个节点最近的节点
自定义IP在PCIE中使用
The use of scrapy crawler framework
Idea common plugins
随机推荐
程序员如何学习开源项目,这篇文章告诉你
rpm和yum
How programmers learn open source projects, this article tells you
STM32个人笔记-程序跑飞
PHP获取时间戳后写数据库的一个问题
在GBase 8c数据库后台,使用什么样的命令来对gtm、dn节点进行主备切换的操作
MySQL query advanced - from the use of functions to table joins, do you remember?
ASP.NET Core 6框架揭秘实例演示[30]:利用路由开发REST API
Naive Bayes--Study Notes--Basic Principles and Code Implementation
Node's traditional and advanced practices for formatting time (moment)
notes....
优炫数据库支持Oracle哪几种时间及日期类型
企业微信群:机器人定时提醒功能数据库配置化
SkiaSharp 之 WPF 自绘 五环弹动球(案例版)
GBase 8s 锁分类
SkiaSharp's WPF self-painted five-ring bouncing ball (case version)
Redis middleware (from building to refuse pit)
对于小应用来讲,使用哪款数据库比较好?
Is the real database data of TiDB stored in kv and pd?
Prime Ring Problem(素数环问题)