C Enumerations and typedef

  1. Enumerations (enum)

1.1 Purpose

Enumerations enhance code readability by grouping related constants into a single type, making intent clearer and reducing magic numbers.

1.2 Syntax

enum enum_type_name {
    CONSTANT_NAME_1,
    CONSTANT_NAME_2,
    CONSTANT_NAME_3
};

1.3 Initialization

Inside an enum, the first constant defaults to 0, and each subsequent constant increments by 1. You can assign explicit values; following constants then continue incrementing from that point.

enum num {
    Mon,       // 0
    Tues,      // 1
    Wednes = 3,// 3
    Thurs,     // 4
    Fri,       // 5
    Satu,      // 6
    Sun        // 7
};

printf("%d ", Mon);
printf("%d ", Thurs);
// Output: 0 4

1.4 Usage

Enum constants are accessible directly without requiring a variable declaration, but variables can be declared to hold enum values.

enum day {
    Mon = 1,
    Tues,
    Wednes,
    Thurs,
    Fri,
    Satu,
    Sun
};

int main() {
    enum day selectedDay;
    // int selectedDay;  // Also valid but less expressive
    printf("Enter a number (1-7): ");
    scanf("%d", &selectedDay);

    switch (selectedDay) {
        case Mon:
            printf("Today is Monday\n");
            break;
        case Tues:
            printf("Today is Tuesday\n");
            break;
        case Wednes:
            printf("Today is Wednesday\n");
            break;
        case Thurs:
            printf("Today is Thursday\n");
            break;
        case Fri:
            printf("Today is Friday\n");
            break;
        case Satu:
            printf("Today is Saturday\n");
            break;
        case Sun:
            printf("Today is Sunday\n");
            break;
        default:
            printf("Invalid input!\n");
            break;
    }
    return 0;
}

1.5 Important Notes

  • Enums in C are implemented as integers internally.
  • Constant names within an enum must be unique and cannot be reused in other enums within the same scope.
  1. Typedef

2.1 Purpose

The typedef keyword creates an alias for an existing data type, improving code clarity and simplifying complex type declarations.

2.2 Usage

2.2.1 Creating Aliases for Primitive Types

This is often used to give descriptive names, e.g., renaming signed char to indicate its size:

typedef signed char int8_t;
typedef signed short int int16_t;
typedef signed int int32_t;

Structures

Without typedef, you must use struct keyword each time:

struct student {
    int a;
    char b;
};
struct student s;

With typedef, you create an alias:

struct student {
    int a;
    char b;
};
typedef struct student stu_t;

// Usage:
stu_t s;

Common simplified form (anonymous struct with typedef):

typedef struct {
    int i;
    char j;
} stu_t;

int main() {
    stu_t s;
    scanf("%d", &s.i);
    printf("%d\n", s.i);
    return 0;
}

Unions

typedef union {
    int integer;
    float floatingPoint;
} Number;

int main() {
    Number myNum;

    myNum.integer = 42;
    printf("Integer value: %d\n", myNum.integer);
    myNum.floatingPoint = 3.14f;
    printf("Floating-point value: %f\n", myNum.floatingPoint);
    return 0;
}
/* Output:
   Integer value: 42
   Floating-point value: 3.14
*/

Enums

typedef enum {
    RED = 1,
    GREEN,
    BLUE
} Color;

int main() {
    Color favoriteColor = GREEN;
    // int favoriteColor = GREEN;  // Equivalent but less descriptive

    printf("Favorite color: %d\n", favoriteColor);
    return 0;
}

Color favoriteColor = GREEN; is functionally identical to int favoriteColor = GREEN;, but the former provides better readability and type safety.

2.3 Comparison of Three Types

When defining structures, unions, or enums, you can omit the tag name and directly attach an alias using typedef, as shown in the simplified examples above.

Tags: C Enum Typedef Struct UNION

Posted on Mon, 05 Oct 2026 16:11:56 +0000 by akjackson1