c 枚举 获得枚举项的字符串
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c 枚举 获得枚举项的字符串相关的知识,希望对你有一定的参考价值。
#include "stdio.h"
void main( )
enum weekday sun,mon,tue,wed,thu,fri,sat day;
int k;
scanf("%d",&k);
day=(enum weekday)k;
switch(day)
case 0: printf("sun\n");break;
case 1: printf("mon\n");break;
case 2: printf("tue\n");break;
case 3: printf("wed\n");break;
case 4: printf("thu\n");break;
case 5: printf("fri\n");break;
case 6: printf("sat\n");break;
default: printf("input error\n");break;
-------------------------------------------
希望达到如上的效果,但是printf中的字符串不是我输入的,而是直接取枚举项中的相应项的字符串
#include "stdio.h"
void main( )
enum weekday sun,mon,tue,wed,thu,fri,sat;
char *weekdaystr[] = "sun","mon","tue","wed","thu","fri","sat";
int k;
scanf("%d",&k);
switch((enum weekday)k)
case 0: printf("%s\\n",weekdaystr[0]);break;
case 1: printf("%s\\n",weekdaystr[1]);break;
case 2: printf("%s\\n",weekdaystr[2]);break;
case 3: printf("%s\\n",weekdaystr[3]);break;
case 4: printf("%s\\n",weekdaystr[4]);break;
case 5: printf("%s\\n",weekdaystr[5]);break;
case 6: printf("%s\\n",weekdaystr[6]);break;
default: printf("input error\\n");break;
本回答被提问者采纳
python 返回带有自然枚举项的字符串
以上是关于c 枚举 获得枚举项的字符串的主要内容,如果未能解决你的问题,请参考以下文章