“C语言中的枚举常量”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) |
Jihongchang(讨论 | 贡献) |
||
第13行: | 第13行: | ||
return 0; | return 0; | ||
} | } | ||
− | </syntaxhighlight>如上,有时候我们需要限制取值的范围。 | + | </syntaxhighlight>如上,有时候我们需要限制取值的范围。<syntaxhighlight lang="c"> |
+ | #include<stdio.h> | ||
+ | |||
+ | enum week { mon = 1, tue = 2, wed = 3, thu = 4, fri = 5, sat = 6, sun = 7 }; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | enum week wx; | ||
+ | wx = mon; | ||
+ | wx = sun; | ||
+ | printf("%d \n", wx); | ||
+ | return 0; | ||
+ | } | ||
+ | </syntaxhighlight><syntaxhighlight lang="console"> | ||
+ | 7 | ||
+ | |||
+ | </syntaxhighlight> |
2022年10月30日 (日) 06:16的版本
https://www.bilibili.com/video/BV1vR4y1H7MY/?p=12
以星期为例:
#include<stdio.h>
// mon = 1, tue = 2, wed = 3, thu = 4, fri = 5, sat = 6, sun = 7
int main()
{
int wx; // 1 2 3 4 5 6 7
wx = 1;
wx = 7;
wx = 9; //实际没有9对应周几
printf("%d \n", wx);
return 0;
}
如上,有时候我们需要限制取值的范围。
#include<stdio.h>
enum week { mon = 1, tue = 2, wed = 3, thu = 4, fri = 5, sat = 6, sun = 7 };
int main()
{
enum week wx;
wx = mon;
wx = sun;
printf("%d \n", wx);
return 0;
}
7