数组指针与多维数组的深入解析
1. 编程基础问题
1.1 枚举类型与条件语句改写
首先,我们要声明一个枚举类型 month_t
,并改写给定的 if
语句。假设 cur_month
是 month_t
类型而非 int
类型。以下是改写后的代码:
#include <stdio.h>
typedef enum {
JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC
} month_t;
int main() {
month_t cur_month = JAN;
if (cur_month == JAN)
printf("Happy New Year\n");
else if (cur_month == JUN)
printf("Summer begins\n");
else if (cur_month == SEP)
printf("Back to school\n");
else if (cur_month == DEC)
printf("Happy Holidays\n");
return 0;
}
等价的 switch
语句如下: