Examples of Eumerator Declaration and Use

The following data type declarations list oats, wheat, barley, corn, and rice as enumeration constants. The number under each constant shows the integer value.

enum grain { oats, wheat, barley, corn, rice };
   /*         0      1      2      3     4         */
enum grain { oats=1, wheat, barley, corn, rice };
   /*         1        2      3      4     5       */
enum grain { oats, wheat=10, barley, corn=20, rice };
   /*          0     10        11     20       21  */

It is possible to associate the same integer with two different enumeration constants. For example, the following definition is valid. The identifiers suspend and hold have the same integer value.

enum status { run, clear=5, suspend, resume, hold=6 };
   /*          0      5        6       7       6       */

The following example is a different declaration of the enumeration tag status:

enum status { run, create, clear=5, suspend };
   /*          0     1        5       6             */

The following program receives an integer as input. The output is a sentence that gives the French name for the weekday that is associated with the integer. If the integer is not associated with a weekday, the program prints "C'est le mauvais jour."

** Example program using enumerations
 **/
 
#include <stdio.h>
 
enum days {
            Monday=1, Tuesday, Wednesday,
            Thursday, Friday, Saturday, Sunday
          } weekday;
 
void french(enum days);
 
int main(void)
{
   int num;
 
   printf("Enter an integer for the day of the week.  "
          "Mon=1,...,Sun=7\n");
   scanf("%d", &num);
   weekday=num;
   french(weekday);
   return(0);
}
void french(enum days weekday)
{
   switch (weekday)
   {
      case Monday:
         printf("Le jour de la semaine est lundi.\n");
         break;
      case Tuesday:
         printf("Le jour de la semaine est mardi.\n");
         break;
      case Wednesday:
         printf("Le jour de la semaine est mercredi.\n");
         break;
      case Thursday:
         printf("Le jour de la semaine est jeudi.\n");
         break;
      case Friday:
         printf("Le jour de la semaine est vendredi.\n");
         break;
      case Saturday:
         printf("Le jour de la semaine est samedi.\n");
         break;
      case Sunday:
         printf("Le jour de la semaine est dimanche.\n");
         break;
      default:
         printf("C'est le mauvais jour.\n");
   }
}


enum Data Type