当前位置:网站首页>016 C语言基础:C语言枚举类型
016 C语言基础:C语言枚举类型
2022-06-27 04:04:00 【入狱计划进度50%】
一:引入
在实际编程中,有些数据的取值往往是有限的,只能是非常少量的整数,并且最好为每个值都取一个名字,以方便在后续代码中使用,比如一个星期只有七天,一年只有十二个月,一个班每周有六门课程等。
以每周七天为例,我们可以使用#define命令来给每天指定一个名字:
#include <stdio.h>
#define Mon 1
#define Tues 2
#define Wed 3
#define Thurs 4
#define Fri 5
#define Sat 6
#define Sun 7
int main(){
int day;
/* 补充知识点: scanf函数称为格式输入函数,即按用户指定的格式从键盘上把数据输入到指定的变量之中。scanf(“格式控制字符串”, 地址表列); 其中,格式控制字符串的作用与printf函数相同,但不能显示非格式字符串,也就是不能显示提示字符串。地址表列中给出各变量的地址。地址是由地址运算符“&”后跟变量名组成的。 */
scanf("%d", &day);
switch(day){
case Mon: puts("Monday"); break;
case Tues: puts("Tuesday"); break;
case Wed: puts("Wednesday"); break;
case Thurs: puts("Thursday"); break;
case Fri: puts("Friday"); break;
case Sat: puts("Saturday"); break;
case Sun: puts("Sunday"); break;
default: puts("Error!");
}
return 0;
}
结果:
┌──(rootkali)-[~/Desktop/c_test]
└─# ./meiju
3
Wednesday
#define命令虽然能解决问题,但也带来了不小的副作用,导致宏名过多,代码松散,看起来总有点不舒服。
C语言提供了一种枚举(Enum)类型,能够列出所有可能的取值,并给它们取一个名字。
二:枚举类型
枚举类型的定义形式为:
enum typeName{
valueName1, valueName2, valueName3, ...};
enum week{
Mon, Tues, Wed, Thurs, Fri, Sat, Sun}; 枚举默认从0开始,往后逐个加1
enum week{
Mon = 1, Tues = 2, Wed = 3, Thurs = 4, Fri = 5, Sat = 6, Sum = 7};
还有一种更为简单的方法是只给第一个名字指定值:enum week{
Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun};与上一行等效。
枚举是一种类型,通过它可以定义枚举变量,enum week a, b, c;
也可以在定义枚举类型的同时定义变量:enum week {
Mon =1, Tues, Wed, Thurs, Fri, Sat, Sun} a, b, c;
有了枚举变量,就可以把列表中的值赋给它:
enum week{
Mon =1, Tues, Wed, Thurs, Fri, Sat, Sun};
enum week a = Mon, b = Wed, c = Sat;
或者:enum week{
Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun } a = Mon, b = Wed, c = Sat;
实例:
#include <stdio.h>
int main(){
enum week{
Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun} day;
scanf("%d", &day);
switch(day){
case Mon: puts("Monday");
break;
case Tues: puts("Tuesday");
break;
case Wed: puts("Wednesday");
break;
case Thurs: puts("Thursday");
break;
case Fri: puts("Friday");
break;
case Sat: puts("Saturday");
break;
case Sun: puts("Sunday");
break;
default: puts("error");
}
return 0;
}
结果:
┌──(rootkali)-[~/Desktop/c_test]
└─# ./meiju2
6
Saturday
需要注意的两点是:
枚举列表中的 Mon、Tues、Wed 这些标识符的作用范围是全局的(严格来说是 main() 函数内部),不能再定义与它们名字相同的变量。
Mon、Tues、Wed 等都是常量,不能对它们赋值,只能将它们的值赋给其他的变量。
边栏推荐
- [BJDCTF2020]The mystery of ip
- Fastdds server records - Translation-
- 2020:MUTANT: A Training Paradigm for Out-of-Distribution Generalizationin Visual Question Answering
- 日志收集系統
- 2021:Graphhopper: Multi-Hop Scene Graph Reasoning for Visual Question Answering
- 2022-06-26:以下golang代码输出什么?A:true;B:false;C:编译错误。 package main import “fmt“ func main() { type
- Anaconda3 is missing a large number of files during and after installation, and there are no scripts and other directories
- Log collection system
- 1.5 conda的使用
- Cultural tourism light show breaks the time and space constraints and shows the charm of night tour in the scenic spot
猜你喜欢

Argo workflows - getting started with kubernetes' workflow engine

2021:Check it again:Progressive Visual Question Answering via Visual Entailment通过视觉暗示进行渐进式视觉问答

2016Analyzing the Behavior of Visual Question Answering Models

JMeter distributed pressure measurement

Kotlin Compose 自定义 CompositionLocalProvider CompositionLocal

Mysql database foundation: DQL data query language

Penetration test - directory traversal vulnerability

卷积神经网络(CNN)网络结构及模型原理介绍

Anaconda3安装过程及安装后缺失大量文件,没有scripts等目录

Products change the world
随机推荐
1.5 use of CONDA
Interview-01
[BJDCTF2020]The mystery of ip
Easy to use plug-ins in idea
ERP demand and sales management Kingdee
Is the truth XX coming? Why are test / development programmers unwilling to work overtime? This is a crazy state
语义化版本 2.0.0
Nestjs environment variable configuration to solve the problem of how to inject services into interceptors
733. image rendering
缓存综合项目--秒杀架构
Knowledge of iPhone certificate structure
文旅灯光秀打破时空限制,展现景区夜游魅力
Basic functions of promise [IV. promise source code]
JMeter takes the result of the previous request as the parameter of the next request
苹果手机证书构体知识
Facing the "industry, University and research" gap in AI talent training, how can shengteng AI enrich the black land of industrial talents?
[数组]BM94 接雨水问题-较难
Nignx configuring single IP current limiting
与STM32或GD32替换说明
渗透测试-文件上传/下载/包含