Enum in C Program: Definition, Usage & Examples | Updated 2025

Learn Enum in C: What It Is and How to Use in Programs

CyberSecurity Framework and Implementation article ACTE

About author

Santhosh (Web Developer )

Santhosh is a C programming instructor who focuses on enumerations (enum) in C and specialises in type-safe constant management and low-level data abstraction. He teaches how to use this powerful feature to write readable, maintainable code. He is skilled in defining named integer constants, customising enum values, and applying enums in control flow and flag operations.

Last updated on 10th Sep 2025| 10373

(5.0) | 32961 Ratings

What is an Enum?

An enum in C, short for enumeration, is a user-defined data type that assigns names to a set of integral constants, making the code more readable and maintainable. Instead of using arbitrary numbers to represent values like states, directions, or options, enums allow programmers to define a collection of named values. To complement such structured logic with intuitive user interfaces, enrolling in Web Designing Training equips you with the skills to translate backend clarity into frontend usability through clean layouts, responsive design, and interactive components. This results in code that is easier to understand and debug. For example, using MONDAY, TUESDAY, etc., is more descriptive than using numbers like 0, 1, and so on.


To Earn Your Web Developer Certification, Gain Insights From Leading Data Science Experts And Advance Your Career With ACTE’s Web Developer Courses Today!


Syntax and Declaration

Declaring an enum in C follows a straightforward syntax. The enum keyword is used, followed by the name of the enumeration and a list of constants enclosed in curly braces. For example, to complement such structured data declarations with high-level container types, exploring Python Collections & How to Master It reveals how Python’s built-in and specialized collections like lists, sets, dictionaries, and the `collections` module enable efficient data organization, manipulation, and access.

  • enum Color {
  • RED,
  • GREEN,
  • BLUE
  • };

This declaration defines RED, GREEN, and BLUE as named constants that by default take on the values 0, 1, and 2 respectively. A variable of this enum type can be declared as: enum Color myColor, It is also common to use typedef to simplify enum usage:

  • typedef enum {
  • RED,
  • GREEN,
  • BLUE
  • } Color;
  • Color myColor;

This approach eliminates the need to use the enum keyword each time you declare a variable.

Default Values and Assignment

When you declare an enum in C, the first identifier in the list is assigned the integer value 0 by default, and the values increase by 1 for each subsequent identifier. For example, to complement such indexed data handling in C with high-level iteration techniques, exploring Python Enumerate & How to Master It reveals how Python’s `enumerate()` function simplifies loop construction by pairing each item in an iterable with its index, enhancing readability and control.

  • enum Direction {
  • NORTH, // 0
  • EAST, // 1
  • SOUTH, // 2
  • WEST // 3
  • };

Once a value is assigned, the next constant will automatically increment from that value unless specified otherwise. This feature is useful for defining non-continuous constants, such as HTTP status codes, device error codes, or signal types.

    Subscribe To Contact Course Advisor

    Using Enums with Switch Statements

    One of the primary advantages of enums is their compatibility with switch statements. This combination allows developers to write more readable conditional logic without using numeric values directly. To align such structured programming practices with career growth, exploring how to Become a Software Enginee offers a roadmap for mastering core concepts, choosing the right programming languages, and building a strong foundation in software development through formal education or coding bootc

    • enum Mode {
    • OFF,
    • ON,
    • STANDBY
    • };
    • void setMode(enum Mode m) {
    • switch (m) {
    • case OFF:
    • printf(“Device is off\n”);
    • break;
    • case ON:
    • printf(“Device is on\n”);
    • break;
    • case STANDBY:
    • printf(“Device is in standby mode\n”);
    • break;
    • }
    • }

    This usage clearly outlines the logic for each state and is much easier to understand and maintain than using raw integer values.


    Would You Like to Know More About Web Developer? Sign Up For Our Web Developer Courses Now!


    Scope and Storage

    In C, the scope of an enum can be either global or local, depending on where it is declared. If declared outside of a function, the enum is globally accessible. If declared inside a function, its scope is limited to that function. Although enums provide symbolic names, they are internally stored as integers.

    Scope and Storage Article

    To complement such backend logic with visually structured front-end design, enrolling in Web Designing Training equips you with the skills to build intuitive interfaces that translate complex data structures into user-friendly experiences. The compiler usually uses the smallest possible integer type that can represent all enum values. However, the C standard does not mandate this, and the actual storage size may vary by compiler.

    Course Curriculum

    Develop Your Skills with Web Developer Certification Course

    Weekday / Weekend BatchesSee Batch Details

    Type Safety and Enums

    Enums in C do not provide strict type safety. This means that enum constants are essentially treated as integer values, and you can assign any integer to an enum variable. To understand how type safety and structured programming evolve in modern development, exploring Java Applications reveals how Java enforces stricter type rules and supports robust application design across domains like enterprise systems, mobile apps, cloud platforms, and embedded technologies.

    • enum Color { RED, GREEN, BLUE };
    • int a = RED; // Valid
    • enum Color c = 5; // Also valid, though semantically incorrect

    This behavior can sometimes lead to bugs if values not defined in the enum are used. In contrast, some modern languages like C++11 introduce enum class to enforce type safety.


    Are You Interested in Learning More About Web Developer? Sign Up For Our Web Developer Courses Today!


    Enumerated Constants

    Enumerated constants are evaluated at compile-time and cannot be altered at runtime. This makes them highly efficient for use in switch-case blocks or conditional logic. They provide better readability than #define constants and are grouped logically. To complement such compile-time efficiency with dynamic text handling, exploring Java String reveals how immutable string objects, powerful methods, and memory optimization techniques form the backbone of robust Java applications.

    Enumerated Constants Article
    • typedef enum {
    • LOW = 1,
    • MEDIUM,
    • HIGH
    • } Severity;

    Enumerated Constants declaration allows you to write code like Severity s = HIGH; which is self-explanatory compared to using numbers.

    Web Development Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

    Comparing Enums with #define

    In C, #define is often used to create symbolic constants. However, comparing enums provides several advantages over #define:

    • Type Checking: Enums provide limited type safety, while #define does not.
    • Scope Control: Enum constants can be limited to function scope; #define is always global.
    • Debugging Support: Enum names appear in debuggers, making debugging easier.
    • Organization: Enums logically group related constants.
    • #define RED 0
    • #define GREEN 1
    • #define BLUE 2
    • // Versus
    • enum Color { RED, GREEN, BLUE };

    Using Comparing Enums makes the code easier to manage, particularly in large projects.

    Nested Enums

    Although C does not support nested enums in the same way as some object-oriented languages, you can simulate nesting by placing enums inside structures. To understand how modern languages handle such structural patterns, exploring Scala vs Python reveals how Scala’s statically typed architecture and Python’s dynamic flexibility approach data modeling, type safety, and code organization from fundamentally different paradigms.

    • struct Device {
    • enum State {
    • INIT,
    • RUNNING,
    • STOPPED
    • } currentState;
    • };

    This approach helps keep related constants encapsulated and avoids name collisions in larger programs.


    Enum in Structs and Functions

    Enums can be used effectively inside structures to define categorical data, and can also be passed to or returned from functions. Here’s an example:

    • typedef enum {
    • MALE,
    • FEMALE
    • } Gender;
    • struct Person {
    • char name[50];
    • Gender gender;
    • };
    • void printGender(Gender g) {
    • if (g == MALE)
    • printf(“Male\n”);
    • else
    • printf(“Female\n”);
    • }

    This technique improves type definition, enforces usage patterns, and makes functions more expressive.


    Conclusion

    Enums in C are a powerful yet simple way to handle groups of related constants. They contribute to more readable, maintainable, and error-resistant code. Although enums in C lack strict type safety, their integration with switch statements and structures makes them highly useful in various programming scenarios. To complement such low-level logic with high-impact visual design, enrolling in Web Designing Training equips you with the tools to build intuitive, user-friendly interfaces that bridge backend functionality with frontend engagement. By leveraging enums, developers can eliminate magic numbers, reduce bugs, and improve the clarity of their logic. With proper understanding and usage, enums can significantly enhance code organization and quality in both small scripts and large applications.

    Upcoming Batches

    Name Date Details
    Web Developer Certification Course

    08 - Sep- 2025

    (Weekdays) Weekdays Regular

    View Details
    Web Developer Certification Course

    10 - Sep - 2025

    (Weekdays) Weekdays Regular

    View Details
    Web Developer Certification Course

    13 - Sep - 2025

    (Weekends) Weekend Regular

    View Details
    Web Developer Certification Course

    14 - Sep - 2025

    (Weekends) Weekend Fasttrack

    View Details