当前位置:网站首页>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个字节.
边栏推荐
- 安装GBase 8c数据库的时候,报错显示“Resource,如何解决?
- Install GBase 8 c database, the error shows "Resource, how to solve?
- 网络基础学习
- sql server, FULL模式, dbcc shrinkfile(2,1) 不能收缩事务日志,还是原来的大小,是为什么?
- Analysis of High Availability Solution Based on MySql, Redis, Mq, ES
- Lsky Pro 企业版手动升级、优化教程
- Opencv creates a window - cv.namedWindow()
- What are the common API security flaws?
- [Beyond programming] When the fig leaf is lifted, when people begin to accept everything
- opencv创建窗口—cv.namedWindow()
猜你喜欢

C language game - minesweeper

HoloView--Customization

mysql login in cmd and basic operations of database and table

The use of scrapy crawler framework

Comprehensive experiment BGP

Microsoft Azure & NVIDIA IoT 开发者季 I|Azure IoT & NVIDIA Jetson 开发基础

VS“无法查找或打开PDB文件”是怎么回事?如何解决

network basic learning

mysql在cmd的登录及数据库与表的基本操作

Opencv creates a window - cv.namedWindow()
随机推荐
SkiaSharp 之 WPF 自绘 五环弹动球(案例版)
《时代》杂志:元宇宙时代将改变世界
程序员如何学习开源项目,这篇文章告诉你
redis
Go-Excelize API source code reading (8) - GroupSheets(sheets []string), UngroupSheets()
HoloView 在 jyputer lab/notebook 不显示总结
[Beyond programming] When the fig leaf is lifted, when people begin to accept everything
sql server, FULL mode, dbcc shrinkfile(2,1) can not shrink the transaction log, or the original size, why?
对于小应用来讲,使用哪款数据库比较好?
STM32个人笔记-看门狗
【应用推荐】常见资源管理器整理,含个人使用体验和产品选型推荐
淘宝商品详情又见淘宝详情,升级高级版 API
sqlserver怎么查询一张表中同人员的交叉日期
Analysis of High Availability Solution Based on MySql, Redis, Mq, ES
网络个各种协议
Ogg synchronizes oracle to mysql, there may be characters that need to be escaped in the field, how to configure escape?
rpm and yum
rpm和yum
Introduction and application of heap memory (including examples)
js中如何实现深拷贝?