L-24 MCS 260 Mon 15 Oct 2001
In this lecture we have seen various examples of enumeration types.
Below is one of the worked examples:
/* L-24 Mon 15 Oct 2001 : an enumeration type for month */
enum month { jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec };
typedef enum month month;
month read_month ( void );
/* asks the user for a number between 1 and 12, returning the month */
void print_month ( month m );
/* prints the full name of the month on screen */
month next_month ( month m );
/* returns the month following m */
#include<stdio.h>
int main ( void )
{
month m;
m = read_month();
printf("current month : ");
print_month(m); printf("\n");
printf("next month : ");
print_month(next_month(m));
printf("\n");
return 0;
}
month read_month ( void )
{
int m;
printf("Give number between 1 and 12 : "); scanf("%d", &m);
return ((month) (m-1));
}
void print_month ( month m )
{
switch(m)
{
case jan: printf("January"); break;
case feb: printf("February"); break;
case mar: printf("March"); break;
case apr: printf("April"); break;
case may: printf("May"); break;
case jun: printf("June"); break;
case jul: printf("July"); break;
case aug: printf("August"); break;
case sep: printf("September"); break;
case oct: printf("October"); break;
case nov: printf("November"); break;
case dec: printf("December"); break;
}
}
month next_month ( month m )
{
return ((month) ((((int) m) + 1) % 12));
}