枚举类型如何循环c
时间: 2023-12-14 07:36:18 浏览: 68
C#中可枚举类型详解
在C语言中,枚举类型本质上是一组带有名称的整数常量,因此可以像循环整数一样循环枚举类型。以下是一个枚举类型的循环示例:
```c
#include <stdio.h>
enum Weekday {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};
int main() {
for (enum Weekday day = Monday; day <= Sunday; day++) {
switch (day) {
case Monday:
printf("Today is Monday.\n");
break;
case Tuesday:
printf("Today is Tuesday.\n");
break;
case Wednesday:
printf("Today is Wednesday.\n");
break;
case Thursday:
printf("Today is Thursday.\n");
break;
case Friday:
printf("Today is Friday.\n");
break;
case Saturday:
printf("Today is Saturday.\n");
break;
case Sunday:
printf("Today is Sunday.\n");
break;
}
}
return 0;
}
```
以上代码将枚举类型 `Weekday` 中的常量从 `Monday` 到 `Sunday` 循环输出,并使用 `switch` 语句根据当前枚举常量输出对应的文本。
阅读全文