Write a c program to illustrate use of data type enum.

 #include <stdio.h>

#include <string.h>


// Example 1: Days of the week (default values: 0, 1, 2, ...)

enum Days {

    SUNDAY,      // 0

    MONDAY,      // 1

    TUESDAY,     // 2

    WEDNESDAY,   // 3

    THURSDAY,    // 4

    FRIDAY,      // 5

    SATURDAY     // 6

};


// Example 2: Months with custom values

enum Months {

    JAN = 1,

    FEB,    // 2

    MAR,    // 3

    APR,    // 4

    MAY,    // 5

    JUN,    // 6

    JUL,    // 7

    AUG,    // 8

    SEP,    // 9

    OCT,    // 10

    NOV,    // 11

    DEC     // 12

};


// Example 3: Traffic light system

enum TrafficLight {

    RED,

    YELLOW,

    GREEN

};


// Example 4: Student grades

enum Grade {

    FAIL = 0,

    PASS = 40,

    SECOND_CLASS = 50,

    FIRST_CLASS = 60,

    DISTINCTION = 75

};


// Example 5: Boolean type using enum

enum Boolean {

    FALSE,

    TRUE

};


// Function to print day name

void printDay(enum Days day) {

    switch(day) {

        case SUNDAY:    printf("Sunday"); break;

        case MONDAY:    printf("Monday"); break;

        case TUESDAY:   printf("Tuesday"); break;

        case WEDNESDAY: printf("Wednesday"); break;

        case THURSDAY:  printf("Thursday"); break;

        case FRIDAY:    printf("Friday"); break;

        case SATURDAY:  printf("Saturday"); break;

        default:        printf("Invalid day");

    }

}


// Function to get traffic light action

void trafficLightAction(enum TrafficLight light) {

    printf("\nTraffic Light: ");

    switch(light) {

        case RED:

            printf("RED - STOP!");

            break;

        case YELLOW:

            printf("YELLOW - Get Ready");

            break;

        case GREEN:

            printf("GREEN - GO!");

            break;

    }

}


// Function to check grade

void checkGrade(int marks) {

    printf("\nMarks: %d - ", marks);

    if (marks >= DISTINCTION)

        printf("Distinction");

    else if (marks >= FIRST_CLASS)

        printf("First Class");

    else if (marks >= SECOND_CLASS)

        printf("Second Class");

    else if (marks >= PASS)

        printf("Pass");

    else

        printf("Fail");

}


int main() {

    printf("========== ENUM DATA TYPE DEMONSTRATION ==========\n");

    

    // Example 1: Using Days enum

    printf("\n***** EXAMPLE 1: DAYS OF THE WEEK *****\n");

    enum Days today = WEDNESDAY;

    enum Days weekend = SATURDAY;

    

    printf("Today is: ");

    printDay(today);

    printf(" (Value: %d)\n", today);

    

    printf("Weekend is: ");

    printDay(weekend);

    printf(" (Value: %d)\n", weekend);

    

    // Example 2: Using Months enum

    printf("\n***** EXAMPLE 2: MONTHS *****\n");

    enum Months currentMonth = MAR;

    printf("Current month: %d (March)\n", currentMonth);

    printf("December value: %d\n", DEC);

    

    // Example 3: Traffic Light System

    printf("\n***** EXAMPLE 3: TRAFFIC LIGHT SYSTEM *****\n");

    enum TrafficLight signal;

    

    for (signal = RED; signal <= GREEN; signal++) {

        trafficLightAction(signal);

    }

    

    // Example 4: Student Grading System

    printf("\n\n***** EXAMPLE 4: STUDENT GRADING SYSTEM *****\n");

    int studentMarks[] = {85, 65, 55, 45, 30};

    

    for (int i = 0; i < 5; i++) {

        checkGrade(studentMarks[i]);

    }

    

    // Example 5: Boolean operations

    printf("\n\n***** EXAMPLE 5: BOOLEAN USING ENUM *****\n");

    enum Boolean isStudent = TRUE;

    enum Boolean isPassed = FALSE;

    

    printf("Is Student: %s (Value: %d)\n", 

           isStudent ? "YES" : "NO", isStudent);

    printf("Is Passed: %s (Value: %d)\n", 

           isPassed ? "YES" : "NO", isPassed);

    

    // Example 6: Using enum in calculations

    printf("\n***** EXAMPLE 6: ENUM IN CALCULATIONS *****\n");

    enum Days startDay = MONDAY;

    enum Days endDay = FRIDAY;

    int workingDays = endDay - startDay + 1;

    printf("Working days from Monday to Friday: %d\n", workingDays);

    

    // Example 7: Array indexing with enum

    printf("\n***** EXAMPLE 7: ARRAY INDEXING WITH ENUM *****\n");

    int workHours[7];

    workHours[MONDAY] = 8;

    workHours[TUESDAY] = 7;

    workHours[WEDNESDAY] = 8;

    workHours[THURSDAY] = 6;

    workHours[FRIDAY] = 8;

    workHours[SATURDAY] = 0;

    workHours[SUNDAY] = 0;

    

    printf("Work hours on Wednesday: %d hours\n", workHours[WEDNESDAY]);

    printf("Total weekly hours: %d hours\n", 

           workHours[MONDAY] + workHours[TUESDAY] + workHours[WEDNESDAY] + 

           workHours[THURSDAY] + workHours[FRIDAY]);

    

    // Summary

    printf("\n\n========== ENUM FEATURES SUMMARY ==========\n");

    printf("1. Enums provide named integer constants\n");

    printf("2. Default values start from 0 and increment by 1\n");

    printf("3. You can assign custom values\n");

    printf("4. Makes code more readable and maintainable\n");

    printf("5. Can be used in switch statements\n");

    printf("6. Can be used as array indices\n");

    printf("7. Helpful for defining states, flags, and options\n");

    

    return 0;

}






Comments