Skip to content
    geeksforgeeks
    • Courses
      • DSA / Placements
      • ML & Data Science
      • Development
      • Cloud / DevOps
      • Programming Languages
      • All Courses
    • Tutorials
      • Python
      • Java
      • ML & Data Science
      • Programming Languages
      • Web Development
      • CS Subjects
      • DevOps
      • Software and Tools
      • School Learning
    • Interview Prep
      • Interview Corner
      • Practice DSA
      • GfG 160
      • Problem of the Day
    • C Tutorial
    • Interview Questions
    • Examples
    • Quizzes
    • Projects
    • Cheatsheet
    • File Handling
    • Multithreading
    • Memory Layout
    • DSA in C
    • C++
    Open In App

    Enumeration (or enum) in C

    Last Updated : 29 Jul, 2025
    Comments
    Improve
    Suggest changes
    504 Likes
    Like
    Report

    In C, an enumeration (or enum) is a user defined data type that contains a set of named integer constants. It is used to assign meaningful names to integer values, which makes a program easy to read and maintain.

    Definition

    An enum must be defined before we can use it in program.

    C
    enum enum_name { 
        n1, n2, n3, ... 
    };
    

    where, n1, n2, n3, ... are names assigned to an integer value. By default, first name n1 is assigned 0, n2 is assigned 1 and the subsequent ones are incremented by 1.

    Example:

    C
    enum calculate { 
        SUM, DIFFERENCE, PRODUCT, QUOTIENT
    };
    

    Here, the name SUM is automatically assigned 0, DIFFERENCE is assigned 1 and so on. Enum names should follow the standard C variable naming rules. Uppercase is preferred for easy identification.

    Also, enum names must be unique in their scope. For example, the below code fails as two enums items and calculate have same named integer PRODUCT.

    C
    enum calculate { 
        SUM, DIFFERENCE, PRODUCT, QUOTIENT
    };
    
    enum item{
        PRODUCT, SERVICE
    };
    

    Create enum Variables

    After enum is defined, we can create variables of that enum by using its specified name.

    C
    enum_name v;
    

    Initialization

    An enum variable can be initialized either with a name defined in the enum definition or directly with its integer value.

    C
    #include <stdio.h>
    
    // Defining enum
    enum direction {
        EAST, NORTH, WEST, SOUTH
    };
    
    int main() {
    
        // Creating enum variable
        enum direction dir = NORTH;
        printf("%d\n", dir);
        
        // This is valid too
        dir = 3;
        printf("%d", dir);
        return 0;
    }
    

    Output
    1
    3

    Though it is recommended to avoid using integers as enums loses their purpose when integer values are preferred in the use.

    Assign Values Manually

    We can also manually assign desired values to the enum names as shown:

    C
    enum enum_name { 
        n1 = val1, n2 = val2, n3, ... 
    };
    

    It is not mandatory to assign all constants. The next name will be automatically assigned a value by incrementing the previous name's value by 1. For example, n3 here will be (val2 + 1). Also, we can assign values in any order, but they must be integer only.

    Example:

    C++
    #include <stdio.h>
    
    // Defining enum
    enum enm {
        a = 3, b = 2, c
    };
    
    int main() {
    
        // Creating enum variable
        enum enm v1 = a;
        enum enm v2 = b;
        enum enm v3 = c;
        printf("%d %d %d", v1, v2, v3);
        return 0;
    }
    

    Output
    3 2 3

    In the above program, a is assigned the value 3, b is assigned 2, and c will automatically be assigned the value 3, as the enum values increment from the last assigned value. We can also confirm here that two enum names can have same value.

    Size of Enums

    Enum definition is not allocated any memory, it is only allocated for enum variables. Usually, enums are implemented as integers, so their size is same as that of int. But some compilers may use short int (or even smaller type) to represent enums. We can use the sizeof operator to check the size of enum variable.

    Example:

    C
    #include <stdio.h>
    
    // Defining enum
    enum direction {
        EAST, NORTH, WEST, SOUTH
    };
    
    int main() {
        enum direction dir = NORTH;
        
        // Checking the size of enum
        printf("%ld bytes", sizeof(dir));
        return 0;
    }
    

    Output
    4 bytes

    Enum and Typedef

    The typedef keyword can be used to define an alias for an enum so that we can avoid using the keyword enum again and again.

    Example:

    C++
    #include <stdio.h>
    
    // Defining enum
    typedef enum direction {
        EAST, NORTH, WEST, SOUTH
    }Dirctn;
    
    int main() {
        Dirctn dir = NORTH;
        
        // Checking the size of enum
        printf("%d", dir);
        return 0;
    }
    

    Output
    1

    In the above program, we have used typedef to create a nickname Dirctn for the direction enum so that we can directly refer to it by using the nickname. It enhances the code readability.

    Applications of Enums in C

    Enums are extensively used in various real-world applications in programming, such as:

    • State Representation: Enums are commonly used to represent different states or modes in state machines, like in game development or process control systems.
    • Error Codes: Enums are useful for defining a set of related error codes with meaningful names, making the code easier to debug and maintain.
    • Menu Choices or User Inputs: Enums are useful for handling menu options or user input and can represent variety of user input such as days of the week.
    • File Permissions: Enums are useful for handling file permissions in an operating system or file management system, with different permission levels such as READ, WRITE, and EXECUTE.
    Create Quiz

    K

    kartik
    Improve

    K

    kartik
    Improve
    Article Tags :
    • C Language
    • C Basics
    • cpp-data-types
    • C-Struct-Union-Enum

    Explore

      C Basics

      C Language Introduction

      6 min read

      Identifiers in C

      3 min read

      Keywords in C

      2 min read

      Variables in C

      4 min read

      Data Types in C

      3 min read

      Operators in C

      8 min read

      Decision Making in C (if , if..else, Nested if, if-else-if )

      7 min read

      Loops in C

      6 min read

      Functions in C

      5 min read

      Arrays & Strings

      Arrays in C

      4 min read

      Strings in C

      5 min read

      Pointers and Structures

      Pointers in C

      7 min read

      Function Pointer in C

      5 min read

      Unions in C

      3 min read

      Enumeration (or enum) in C

      5 min read

      Structure Member Alignment, Padding and Data Packing

      8 min read

      Memory Management

      Memory Layout of C Programs

      5 min read

      Dynamic Memory Allocation in C

      7 min read

      What is Memory Leak? How can we avoid?

      2 min read

      File & Error Handling

      File Handling in C

      11 min read

      Read/Write Structure From/to a File in C

      3 min read

      Error Handling in C

      8 min read

      Using goto for Exception Handling in C

      4 min read

      Error Handling During File Operations in C

      5 min read

      Advanced Concepts

      Variadic Functions in C

      5 min read

      Signals in C language

      5 min read

      Socket Programming in C

      8 min read

      _Generics Keyword in C

      3 min read

      Multithreading in C

      9 min read
    top_of_element && top_of_screen < bottom_of_element) || (bottom_of_screen > articleRecommendedTop && top_of_screen < articleRecommendedBottom) || (top_of_screen > articleRecommendedBottom)) { if (!isfollowingApiCall) { isfollowingApiCall = true; setTimeout(function(){ if (loginData && loginData.isLoggedIn) { if (loginData.userName !== $('#followAuthor').val()) { is_following(); } else { $('.profileCard-profile-picture').css('background-color', '#E7E7E7'); } } else { $('.follow-btn').removeClass('hideIt'); } }, 3000); } } }); } $(".accordion-header").click(function() { var arrowIcon = $(this).find('.bottom-arrow-icon'); arrowIcon.toggleClass('rotate180'); }); }); window.isReportArticle = false; function report_article(){ if (!loginData || !loginData.isLoggedIn) { const loginModalButton = $('.login-modal-btn') if (loginModalButton.length) { loginModalButton.click(); } return; } if(!window.isReportArticle){ //to add loader $('.report-loader').addClass('spinner'); jQuery('#report_modal_content').load(gfgSiteUrl+'wp-content/themes/iconic-one/report-modal.php', { PRACTICE_API_URL: practiceAPIURL, PRACTICE_URL:practiceURL },function(responseTxt, statusTxt, xhr){ if(statusTxt == "error"){ alert("Error: " + xhr.status + ": " + xhr.statusText); } }); }else{ window.scrollTo({ top: 0, behavior: 'smooth' }); $("#report_modal_content").show(); } } function closeShareModal() { const shareOption = document.querySelector('[data-gfg-action="share-article"]'); shareOption.classList.remove("hover_share_menu"); let shareModal = document.querySelector(".hover__share-modal-container"); shareModal && shareModal.remove(); } function openShareModal() { closeShareModal(); // Remove existing modal if any let shareModal = document.querySelector(".three_dot_dropdown_share"); shareModal.appendChild(Object.assign(document.createElement("div"), { className: "hover__share-modal-container" })); document.querySelector(".hover__share-modal-container").append( Object.assign(document.createElement('div'), { className: "share__modal" }), ); document.querySelector(".share__modal").append(Object.assign(document.createElement('h1'), { className: "share__modal-heading" }, { textContent: "Share to" })); const socialOptions = ["LinkedIn", "WhatsApp","Twitter", "Copy Link"]; socialOptions.forEach((socialOption) => { const socialContainer = Object.assign(document.createElement('div'), { className: "social__container" }); const icon = Object.assign(document.createElement("div"), { className: `share__icon share__${socialOption.split(" ").join("")}-icon` }); const socialText = Object.assign(document.createElement("span"), { className: "share__option-text" }, { textContent: `${socialOption}` }); const shareLink = (socialOption === "Copy Link") ? Object.assign(document.createElement('div'), { role: "button", className: "link-container CopyLink" }) : Object.assign(document.createElement('a'), { className: "link-container" }); if (socialOption === "LinkedIn") { shareLink.setAttribute('href', `https://www.linkedin.com/sharing/share-offsite/?url=${window.location.href}`); shareLink.setAttribute('target', '_blank'); } if (socialOption === "WhatsApp") { shareLink.setAttribute('href', `https://api.whatsapp.com/send?text=${window.location.href}`); shareLink.setAttribute('target', "_blank"); } if (socialOption === "Twitter") { shareLink.setAttribute('href', `https://twitter.com/intent/tweet?url=${window.location.href}`); shareLink.setAttribute('target', "_blank"); } shareLink.append(icon, socialText); socialContainer.append(shareLink); document.querySelector(".share__modal").appendChild(socialContainer); //adding copy url functionality if(socialOption === "Copy Link") { shareLink.addEventListener("click", function() { var tempInput = document.createElement("input"); tempInput.value = window.location.href; document.body.appendChild(tempInput); tempInput.select(); tempInput.setSelectionRange(0, 99999); // For mobile devices document.execCommand('copy'); document.body.removeChild(tempInput); this.querySelector(".share__option-text").textContent = "Copied" }) } }); // document.querySelector(".hover__share-modal-container").addEventListener("mouseover", () => document.querySelector('[data-gfg-action="share-article"]').classList.add("hover_share_menu")); } function toggleLikeElementVisibility(selector, show) { document.querySelector(`.${selector}`).style.display = show ? "block" : "none"; } function closeKebabMenu(){ document.getElementById("myDropdown").classList.toggle("show"); }
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Campus Training Program
  • Explore
  • POTD
  • Job-A-Thon
  • Blogs
  • Nation Skill Up
  • Tutorials
  • Programming Languages
  • DSA
  • Web Technology
  • AI, ML & Data Science
  • DevOps
  • CS Core Subjects
  • Interview Preparation
  • Software and Tools
  • Courses
  • ML and Data Science
  • DSA and Placements
  • Web Development
  • Programming Languages
  • DevOps & Cloud
  • GATE
  • Trending Technologies
  • Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
  • Preparation Corner
  • Interview Corner
  • Aptitude
  • Puzzles
  • GfG 160
  • System Design
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences