c语言枚举变量作为函数参数
时间: 2024-01-06 21:24:48 浏览: 299
指针变量作为函数的参数-C语言入门教程
枚举变量作为函数参数可以通过传递枚举类型的参数来实现。下面是一个示例代码:
```c
#include <stdio.h>
// 定义一个枚举类型
enum Weekday {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
};
// 函数接受枚举类型的参数
void printWeekday(enum Weekday 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;
default:
printf("Invalid day.\n");
break;
}
}
int main() {
enum Weekday today = Tuesday;
printWeekday(today); // 输出:Today is Tuesday.
return 0;
}
```
在上面的示例中,我们定义了一个枚举类型`Weekday`,然后在`printWeekday`函数中接受一个枚举类型的参数`day`,根据传入的参数值打印对应的星期几。在`main`函数中,我们定义了一个枚举变量`today`并赋值为`Tuesday`,然后将其作为参数传递给`printWeekday`函数进行打印。
阅读全文