当前位置:网站首页>C语言自定义类型 — 枚举
C语言自定义类型 — 枚举
2022-07-24 17:29:00 【eat_sleep_play( )】

这次带着大家学习了枚举类型的定义、优点以及使用。
下一篇将带着大家学习联合体方面的知识。
目录
枚举
枚举其实就是一一列举的意思,把可能的取值一一列举。
例如:
- 一周的星期一到星期天一共有7天,可以一一列举。
- 性别有男、女人、保密也可以一一列举。
- 月份有12个月,也可以一一列举。
取值是有限的。
1.枚举类型的定义
例1:
enum Day//星期
{
Mon,
Tous,
Wed,
Thur,
Fri,
Sat,
Sun
};
struct stu
{
int a;
char b;
};- enum 是枚举关键字,Day 是枚举的标签;struct 是结构体关键字,stu 是结构体的标签。
- 两者都有1个{}和分号,结构体的叫成员变量,枚举的叫枚举常量。
- 在语法形式上,枚举都是一逗号结尾的,而结构体则是分号。
例2:
enum Day//星期
{
Mon,
Tous,
Wed,
Thur,
Fri,
Sat,
Sun
};
int main()
{
enum Day d = Fri;
return 0;
}- 此时的d表示的是星期五的意思,而且d的值只能是枚举类型其中的一个。
- {} 里面的内容是枚举类型的可能取值。
枚举类型的可能取值都是有值的,默认从0开始,一次递增1,当然在定义的时候也可以赋初值。
例3:
enum Day//星期
{
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;
}代码结果:

例4:
修改枚举的默认值
enum Day//星期
{
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;
}代码结果:

2.枚举类型的优点
为什么要使用枚举呢
枚举的优点:
- 增加代码的可读性和可维护性
- 和#define定义的标识符比较枚举有类型检查,更加严谨
- 防止命名污染(封装)
- 便于调试
- 使用方便,一次可以定义多个常量
3.枚举类型的使用
enum Color//颜色
{
RED = 1,
GREEN = 2,
BLUE = 4
};
int main()
{
enum Color color = GREEN;//只能拿枚举常量给枚举变量赋值,才不会出现类型的差异。
color = 5; //error
return 0;
}边栏推荐
- Getaverse, a distant bridge to Web3
- What is the meaning of void 0? Is undefined changeable?
- Scroll bar adjust brightness and contrast
- 实习报告1——人脸三维重建方法
- Make good use of these seven tips in code review, and it is easy to establish your opposition alliance
- 2022 Niuke summer multi school K - link with bracket sequence I (linear DP)
- A problem of MySQL database
- UFW port forwarding
- hcip第三天
- 2022年最新浙江建筑安全员模拟题库及答案
猜你喜欢

Tensorflow introductory tutorial (40) -- acunet

NC port forwarding

Method of querying comma separated strings in a field by MySQL

Two dimensional convolution -- use of torch.nn.conv2d

2022 Yangtze River Delta industrial automation exhibition will be held in Nanjing International Exhibition Center in October

Apachecon Asia 2022 opens registration: pulsar technology issues make a big debut

Still developing games with unity? Then you're out. Try unity to build an answer system

UFW port forwarding

Iftnews | Christie's launched its venture capital department, aiming at Web3 and metauniverse industries

List of stringutils and string methods
随机推荐
地表最强程序员装备“三件套”,你知道是什么吗?
2022 牛客暑期多校 K - Link with Bracket Sequence I(线性dp)
Six ways for JS to implement inheritance
Getaverse,走向Web3的远方桥梁
Ipaylinks, a cross-border payment integration service, won the 3A Asia Award of treasury
[spoken English] 01 - Introduction to atom
JS image conversion Base64 Base64 conversion to file object
Analyze the capabilities and scenarios of Apache pulsar, a cloud native message flow system
Atcoder Beginner 202 E - Count Descendants(离线查询 重链剖分树上启发式合并)
Class bytecode file
Iftnews | Christie's launched its venture capital department, aiming at Web3 and metauniverse industries
[matlab]: basic knowledge learning
Summary of ROS master-slave communication experience
DHCP relay of HCNP Routing & Switching
What is the meaning of void 0? Is undefined changeable?
还在用Xshell?你out了,推荐一个更现代的终端连接工具!
Iqiyi Tiktok reconciled, Weibo lying gun?
opencv自带颜色操作
Live review | wonderful playback of Apache pulsar meetup (including PPT download)
Work with growingio engineers this time | startdt Hackathon