Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

C Enumerations enum Declaration: Tutorial, Examples, FAQs & Interview Tips

What is an enum?

An enum (enumeration) is a user-defined type consisting of named integer constants. Enums make code more readable by replacing magic numbers with meaningful names.

// Define an enum
enum Day { MON, TUE, WED, THU, FRI, SAT, SUN };
//         0    1    2    3    4    5    6   (default values)

// Declare a variable
enum Day today = WED;
printf("%d\n", today);  // 2

Custom Values

You can assign custom integer values to enum constants. Subsequent constants increment from the last assigned value.

enum Status {
    PENDING  = 0,
    ACTIVE   = 1,
    INACTIVE = 2,
    DELETED  = 99
};

enum Color {
    RED   = 1,
    GREEN = 2,
    BLUE  = 4   // powers of 2 for bit flags
};

typedef enum

Using typedef with enum lets you declare variables without the enum keyword each time.

typedef enum { LOW, MEDIUM, HIGH } Priority;
Priority p = HIGH;  // no need to write 'enum Priority'
Basic enum - Days of Week
#include <stdio.h>

enum Day { MON=1, TUE, WED, THU, FRI, SAT, SUN };

const char* dayName(enum Day d) {
    switch (d) {
        case MON: return "Monday";
        case TUE: return "Tuesday";
        case WED: return "Wednesday";
        case THU: return "Thursday";
        case FRI: return "Friday";
        case SAT: return "Saturday";
        case SUN: return "Sunday";
        default:  return "Unknown";
    }
}

int main() {
    enum Day today = WED;
    printf("Today is %s (value: %d)\n", dayName(today), today);

    // Iterate over all days
    printf("\nAll days:\n");
    for (enum Day d = MON; d <= SUN; d++) {
        printf("  %d: %s\n", d, dayName(d));
    }

    // Check if it's a weekend
    if (today == SAT || today == SUN) {
        printf("\nIt's the weekend!\n");
    } else {
        printf("\nIt's a weekday.\n");
    }

    return 0;
}

/*
Today is Wednesday (value: 3)

All days:
  1: Monday
  2: Tuesday
  ...
  7: Sunday

It's a weekday.
*/
enum with Custom Values, typedef enum and switch
#include <stdio.h>

// typedef enum - no need to write 'enum' when declaring variables
typedef enum {
    HTTP_OK        = 200,
    HTTP_NOT_FOUND = 404,
    HTTP_ERROR     = 500
} HttpStatus;

typedef enum {
    NORTH = 0,
    EAST  = 90,
    SOUTH = 180,
    WEST  = 270
} Direction;

void handleStatus(HttpStatus status) {
    switch (status) {
        case HTTP_OK:
            printf("200 OK - Request successful\n");
            break;
        case HTTP_NOT_FOUND:
            printf("404 Not Found - Resource missing\n");
            break;
        case HTTP_ERROR:
            printf("500 Internal Server Error\n");
            break;
        default:
            printf("Unknown status: %d\n", status);
    }
}

int main() {
    HttpStatus s = HTTP_NOT_FOUND;
    handleStatus(s);
    handleStatus(HTTP_OK);

    Direction dir = EAST;
    printf("\nHeading: %d degrees\n", dir);

    // Enum in array
    Direction path[] = {NORTH, EAST, SOUTH, WEST};
    const char *names[] = {"North", "East", "South", "West"};
    printf("Path: ");
    for (int i = 0; i < 4; i++) {
        printf("%s(%d) ", names[i], path[i]);
    }
    printf("\n");

    return 0;
}

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.