当前位置:网站首页>C语言初阶(九)枚举
C语言初阶(九)枚举
2022-06-21 10:55:00 【咔咔公主】
一.常量符号化
1.const
const double PI = 3.1415926;
// 星期
const int SUN = 0;
const int MON = 1;
const int TUES = 2;
const int WED = 3;
const int THUR = 4;
const int FRI = 5;
const int SAT = 6;2.#define
#define PI 3.1415926
// 星期
#define SUM 0
#define MON 1
#define TUES 2
#define WED 3
#define THUR 4
#define FRI 5
#define SAT 63.枚举
作用域
#include <stdio.h>
void func (){
#define N 12
const int n = 12;
}
int main(){
printf("%d\n",N);
//printf("%d\n",n);
}二.枚举
1.是什么
枚举是一种用户定义的数据类型,枚举可以看作是一组宏定义。
2.怎么用
enum 枚举类型名{名字0,名字1,名字2,...,名字n};枚举大括号里面的名字是常量符号,类型为int,值依次从0到n。
枚举就是给这些常量值,规定一个名字。
枚举量可以直接作为值使用。
枚举类型可以直接作为类型使用。
#include <stdio.h>
enum Mouth{Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sept,Oct,Nov,Dec};
int month_days[]={31,28,31,30,31,30,31,31,30,31,30,31};
char* month_names[]={"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
void PrintMonth(enum Mouth m){
printf("%s有%d天\n",month_names[m],month_days[m]);
}
void main(){
enum Mouth m = Jan;
PrintMonth(m);
}声明枚举时可以指定值
// 常用进制
enum Radix{Bin=2,Oct=8,Dec=10,Hex=16};也可以其中一个值,后续值依次加1
enum Mouth{Jan=1,Feb,Mar,Apr,May,Jun,Jul,Aug,Sept,Oct,Nov,Dec};边栏推荐
- The more AI evolves, the more it resembles the human brain! Meta found the "prefrontal cortex" of the machine. AI scholars and neuroscientists were surprised
- 芯片供给过剩迹象明显,ASML不再是香饽饽,投资机构大举做空
- cvte一面
- Dapr advanced -02- view dapr logs
- FastAPI Web框架 [Pydantic]
- What securities does qiniu use to open an account? Is it safe to open an account
- Concept of naive Bayes
- 04. New features of redis: Interpretation of multithreading model
- Solon 1.8.3 release, cloud native microservice development framework
- 详解连接池参数设置(边调边看)
猜你喜欢

High concurrency - personal notes

IAAs, SaaS, PAAS, DAAS concepts

Xidian AI ranked higher than Qingbei in terms of AI major, and Nantah ranked first in China in terms of Software Science in 2022

为什么 C# 访问 null 字段会抛异常?

实测:云RDS MySQL性能是自建的1.6倍

Tensorflow, danger! Google itself is the one who abandoned it

WCF RestFul+JWT身份验证

Map集合遍历,添加替换,删除元素

中国国际电子商务中心与易观分析联合发布:2021年4季度全国网络零售发展指数同比增长0.6%

东方甄选双语直播火爆出圈,新东方转型初见端倪
随机推荐
What USB driver needs to know
开源FTP 服务器 FileZilla Server
Add solid state to the computer
[Architect (Part 38)] locally install the latest version of MySQL database developed by the server
Leader: who uses redis expired monitoring to close orders? Get out of here!
3000帧动画图解MySQL为什么需要binlog、redo log和undo log
The most powerful eight part essay in 2022, "code out eight part essay - cut out the offer line"
一行代码加速 sklearn 运算上千倍
Esp8266/esp32 +1.3 "or 0.96" IIC OLED pointer clock
Start from scratch 10- background management system development
ThreadLocal
简易的安卓天气app(三)——城市管理、数据库操作
Quick sorting, simple and easy to understand principle description, with C code implementation,
Kotlin - I/O流
MySQL 5.7 is about to be stopped and only maintained. It's time to learn a wave of MySQL 8
一款完整开源的物联网基础平台
06. Redis log: the trump card for fast recovery without fear of downtime
Yum source server configuration
Mqtt of NLog custom target
Do website from scratch 11- blog development