当前位置:网站首页>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};边栏推荐
- 为什么 C# 访问 null 字段会抛异常?
- STL summary
- 我国服装纺织制造业面临着异常严峻的挑战
- 知识点滴 - 什么是加速移动网页(AMP)?
- Mythical games announced its cooperation with kakao games, a leading Korean game publisher, to promote business expansion in the Asia Pacific Region
- 中芯国际据理力争赢了美国诉讼,证明独立自主才能有更长远的发展
- WPF DataContext usage
- The "first city" in Central China. How can Changsha be built?
- Swift 之返回按钮
- Feature metric loss for self supervised learning of depth and egomotion
猜你喜欢

Running view of program

IAAs, SaaS, PAAS, DAAS concepts

WCF RestFul+JWT身份验证

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

Solon 1.8.3 发布,云原生微服务开发框架

05. Redis core chapter: the secret that can only be broken quickly

Huawei releases wireless innovative products and solutions to build 5gigaverse Society

JS regular - comb

DSP online upgrade (3) -- how to burn two projects in the on-chip flash of a DSP chip

Citus 11 for Postgres is completely open source and can be queried from any node (citus official blog)
随机推荐
Optimisation des performances - compression, chargement et formatage des images
The advanced process resistance of Intel and TSMC is increasing, and Chinese chips are expected to shorten the gap
Concept of naive Bayes
ES复合查询工作量评估
JS regular - comb
Quickly analyze oom using mat tools
Dapr advanced -02- view dapr logs
Leader: who uses redis expired monitoring to close orders? Get out of here!
New year's Eve, are you still changing the bug?
STL summary
Use of jobservice
2. MySQL index creation method and its optimization
Simple implementation of Snake game in C language
Redis core: usage specification
Eig and Saudi Aramco signed a memorandum of understanding to expand energy cooperation
[ZZ] OWT server: audio and video forwarding diagram
leetcode 第一题——两数之和
Answers to mobile application development learning general test questions
The first phase of takin open source training camp is coming! Teach you to complete the full link voltage test hand in hand!
Citus 11 for Postgres is completely open source and can be queried from any node (citus official blog)