当前位置:网站首页>C language custom types - Enumeration
C language custom types - Enumeration
2022-07-24 17:39:00 【eat_ sleep_ play( )】
Consortium :http://t.csdn.cn/xPGVu
Structure : http://t.csdn.cn/vVtua

This time I took you to learn the definition of enumeration type 、 Advantages and use .
The next article will take you to learn about the consortium .
Catalog
1. Definition of enumeration type
2. Advantages of enumerating types
enumeration
Enumeration actually means enumerating one by one , List the possible values one by one .
for example :
- From Monday to Sunday, there are 7 God , You can list them one by one .
- Gender has male 、 A woman 、 Confidentiality can also be listed one by one .
- Month has 12 Months , You can also list them one by one .
The value is limited .
1. Definition of enumeration type
example 1:
enum Day// week
{
Mon,
Tous,
Wed,
Thur,
Fri,
Sat,
Sun
};
struct stu
{
int a;
char b;
};- enum Is an enumeration keyword ,Day Is the tag of enumeration ;struct Is the structure keyword ,stu Is the label of the structure .
- Both have 1 individual {} And semicolon , The structure is called member variable , Enumerations are called enumerating constants .
- In grammatical form , Enumerations are all comma terminated , The structure is semicolon .
example 2:
enum Day// week
{
Mon,
Tous,
Wed,
Thur,
Fri,
Sat,
Sun
};
int main()
{
enum Day d = Fri;
return 0;
}- At this time d It means Friday , and d The value of can only be one of the enumeration types .
- {} The contents are the possible values of enumeration types .
All possible values of enumeration types have values , The default from the 0 Start , One increment 1, Of course, initial values can also be assigned when defining .
example 3:
enum Day// week
{
Mon,//0
Tous,//1
Wed,//2
Thur,//3
Fri,//4
Sat,//5
Sun//6
};
int main()
{
printf("%d\n", Mon);
printf("%d\n", Tous);
printf("%d\n", Wed);
return 0;
}Code results :

example 4:
Modify the default value of enumeration
enum Day// week
{
Mon = 1,
Tous,
Wed,
Thur,
Fri,
Sat,
Sun
};
int main()
{
//enum Day d = Fri;
printf("%d\n", Mon);
printf("%d\n", Tous);
printf("%d\n", Wed);
printf("%d\n", Thur);
printf("%d\n", Fri);
printf("%d\n", Sat);
printf("%d\n", Sun);
return 0;
}Code results :

2. Advantages of enumerating types
Why use enumeration
Advantages of enumeration :
- Increase the readability and maintainability of the code
- and #define The defined identifier comparison enumeration has type checking , More rigorous
- Prevent naming pollution ( encapsulation )
- Easy to debug
- Easy to use , You can define more than one constant at a time
3. Use of enumeration types
enum Color// Color
{
RED = 1,
GREEN = 2,
BLUE = 4
};
int main()
{
enum Color color = GREEN;// Only enumeration constants can be assigned to enumeration variables , There will be no type difference .
color = 5; //error
return 0;
}边栏推荐
- Step by step introduction to the development framework based on sqlsugar (12) -- split the content of the page module into components to realize the division and rule processing
- Eth POS 2.0 stacking test network pledge process
- opencv自带颜色操作
- Work with growingio engineers this time | startdt Hackathon
- Class bytecode file
- Development Series III of GaN (lapgan, srgan)
- Hcip fourth day notes
- 2022 牛客暑期多校 K - Link with Bracket Sequence I(线性dp)
- OpenCV 图片旋转
- C语言编程训练题目:左旋字符串中的k个字符、小乐乐与欧几里得、打印箭型图案、公务员面试、杨树矩阵
猜你喜欢

Wrote a few small pieces of code, broke the system, and was blasted by the boss

Atcoder beginer 202 e - count descendants (heuristic merge on heavy chain split tree for offline query)

Today, I met a 38K from Tencent, which let me see the ceiling of the foundation

C语言自定义类型讲解 — 结构体

Eth POS 2.0 stacking test network pledge process

UFW port forwarding

滚动条调整亮度和对比度

Hcip day 3

nc 端口转发

OpenCV 图片旋转
随机推荐
Detailed explanation of ansible automatic operation and maintenance (V) the setting and use of variables in ansible, the use of jinja2 template and the encryption control of ansible
The most powerful programmer on the earth is equipped with a "three piece set". Do you know what it is?
Use yarn
Image information is displayed by browser: data:image/png; Base64, + image content
Opencv has its own color operation
es(1)
Scroll bar adjust brightness and contrast
ROC and AUC details of the recommended system
Heuristic merging (including examples of general formula and tree heuristic merging)
Is it safe for qiniu to open an account?
Link editing tips of solo blog posts illegal links
2022年最新浙江建筑安全员模拟题库及答案
It's time to consider slimming down your app
Demonstration experiment of scrollbar for adjusting image brightness
Today, I met a 38K from Tencent, which let me see the ceiling of the foundation
Tensorflow introductory tutorial (40) -- acunet
[wechat official account H5] authorization
Canvas from getting started to persuading friends to give up (graphic version)
nc 端口转发
Kyligence attended the Huawei global smart finance summit to accelerate the expansion of the global market