当前位置:网站首页>Custom types: Enumeration
Custom types: Enumeration
2022-07-24 16:38:00 【Yuan_ o_】
3. enumeration
Enumeration, as the name suggests, is to enumerate one by one
List the possible values one by one
For example, in our life :
A week's Monday to Sunday is limited 7 God , You can list them one by one
Gender : male , Woman , A secret , You can also list them one by one
Month has 12 Months , You can also list them one by one
These can be enumerated
3.1 Definition of enumeration type
enum Day// week
{
Mon,
Tues,
Wed,
Thur,
Fri,
Sat,
Sun
};
enum Sex// Gender
{
MALE,
FEMALE,
SECRET
};
enum Color// Color
{
RED,
GREEN,
BLUE
};
As defined above enum Day,enum Sex,enum Color All enumeration types .
{ } The contents in are possible values of enumeration types , Also called Enumeration constants .
These constants have not been stored in memory ( It is now a type , Is a drawing , Do not occupy memory ), Only when they are assigned to some variables will they be stored in memory , Such as :enum Day d = Fri;
Be careful :
Enumeration constants have values , The default from the 0 Start , In turn, increasing 1, Such as :
#include <stdio.h>
enum Day
{
Mon,
Tues,
Wed,
Thur,
Fri,
Sat,
Sun
};
int main()
{
printf("%d\n", Mon);
printf("%d\n", Tues);
printf("%d\n", Wed);
printf("%d\n", Thur);
printf("%d\n", Fri);
printf("%d\n", Sat);
printf("%d\n", Sun);
return 0;
}

Of course, initial values can also be assigned when defining
for example :
enum Color// Color
{
RED=1,
GREEN=2,
BLUE=4
};
3.2 Advantages of enumeration
Why use enumeration ?
We can use #define Define constants , Why do I have to use enumeration ?
Advantages of enumeration :
- Increase the readability and maintainability of the code
- and #define Defined identifier , Enumeration type checking is more rigorous
- Prevent named pollution ( encapsulation )
- Easy to debug
- Easy to use , You can define more than one constant at a time
3.3 Use of enumeration
enum Color// Color
{
RED=1,
GREEN=2,
BLUE=4
};
enum Color clr = GREEN;// Only enumeration constants can be assigned to enumeration variables , There will be no type difference .
clr = 5; //ok??
The above code is in C The program can run , stay C++ Not in grammar . Because enumeration is a type , stay C++ In grammar , You can't put one int Type is assigned to enumeration type .
边栏推荐
- MODIS 16 day data monthly / quarterly synthesis
- MODIS data WGet Download
- Telephone system rules
- ZBar project introduction and installation configuration| [email protected]
- Jing Wei PS tutorial: basic part a
- EF LINQ Miscellany
- Microsoft Visio professional 2013 cannot be opened or uninstalled. Solution
- 自定义类型:枚举
- Wentai technology and Baoxin software deepened 5g cooperation, and 5g manufacturing settled in Baowu, China
- Custom view - Custom button
猜你喜欢

TCP protocol debugging tool tcpengine v1.3.0 tutorial

剑指 Offer 48. 最长不含重复字符的子字符串

Sword finger offer 22. the penultimate node in the linked list

1184. 公交站间的距离

ArcGIS layer annotation display

ARP 入门

会议OA项目进度(一)

Mcd12q1 data shows multiple classifications in envi

How to prevent XSS in PHP

如何防止跨站点脚本 (XSS) 攻击完整指南
随机推荐
Minor record
ArcGIS pixel size changed from 0.00025 to meters
MySQL converts strings to numeric types and sorts them
ArcGIS layer annotation display
Intel plans to sell baseband chip business, but Apple has given up?
Meeting OA project progress (II)
jvm类加载子系统
Disassembly of Samsung Galaxy fold: the interior is extremely complex. Is the hinge the main cause of screen damage?
【技术】uniapp之聊天室 demo
Unity 相机自由移动控制
Best practices for preventing XSS in PHP web applications
Qt键盘事件(一)——检测按键输入
Wentai technology and Baoxin software deepened 5g cooperation, and 5g manufacturing settled in Baowu, China
Microsoft Visio professional 2013 cannot be opened or uninstalled. Solution
【技术】uniapp 之 在线选座 demo
Unity camera free movement control
[leetcode]38. counting - problem solving (execution time beat 91%, memory consumption beat 97%)
Simple QQ? QT can also be achieved! (I)
Multithreading (basic)
QT design simulation robot controller