C语言学习九枚举的定义和应用

枚举的定义

# include <stdio.h>

enum WeekDay
{
	MonDay, TuesDay, WednesDay, ThursDay, FriDay, SaturDay, SunDay	
};

int main(void)
{	
	//int day;	//day定义成int类型不合适
	enum WeekDay day = WednesDay;
	printf("%d\n", day);
	
	return 0;
}

 

枚举的简单应用

# include <stdio.h>

enum weekday
{
	MonDay, TuesDay, WednesDay, ThursDay, FriDay, SaturDay, SunDay	
};

void f(enum weekday i) //本函数的目的只是期望接受0--6之间的数字,将形参i定义为枚举
{
	switch (i)
	{
	case 0:
		printf("MondDay!\n");
		break;
	case 1:
		printf("TuesDay!\n");
		break;
	case 2:
		printf("WednesDay!\n");
		break;
	case 3:
		printf("ThursDay!\n");
		break;
	case 4:
		printf("FriDay!\n");
		break;
	case 5:
		printf("SaturDay!\n");
		break;
	case 6:
		printf("SunDay!\n");
		break;
	}
}

int main(void)
{	
	f(FriDay);		//虽然FriDay本质上就是5,但直接写成f(5); 就是错的

	return 0;
}

 

你可能感兴趣的:(c)