33,311
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
typedef enum {A=1,B,C,D} SchoolName;
const char *e2s(int e) {
static const char e2str[5][2]={"0","A","B","C","D"};
if (!(1<=e && e<=4)) e=0;
return e2str[e];
}
int main(int argc ,char * argv[])
{
SchoolName school=0;
while (1) {
printf("enter school(1|2|3|4):\n");
rewind(stdin);
if (1==scanf("%d",(int *)&school)) {
if (1<=school && school<=4) break;
}
}
printf("school==%d==%s\n",school,e2s(school));
return 0;
}
[feng@other #15]$cat a.c
#include<stdio.h>
typedef enum {A=1,B,C,D} SchoolName;
int main(int argc ,char * argv[])
{
SchoolName school=0;
printf("enter school\n");
scanf("%d",&school);
printf("school=%d\n",school);
return 0;
}
[feng@other #16]$make
gcc -o a a.c
[feng@other #17]$./a
enter school
3
school=3
[feng@other #18]$