Enums in C

enum

What is Enum ?

Enumeration in C, helps the programmer to create user defined data-types, to make the code more easy and readable. Enum is almost similar to structure, as it can hold any data-type, which the programmer may want, this is advantageous when the program gets complicated or when more than one programmer would be working on it, for the sake of better understanding.  Enum saves time. Make the program more readable To define enums, the enum keyword is used enum variables can also take their corresponding integral value, starting from zero. 

How to declare a enum ?

Declaring a enum is almost similar to declare a structure. We can declare a enum using the keyword “enum” followed by the enum name, and than stating its elements with the curly braces “{}”, separated by commas.   

enum color {
red,
green,
blue
};
  • The first part of the above declaration, declares the data type and specifies its possible values. These values are called “enumerators”.
  • The second part declares variables of this data-types

How does enum works ?

The variables declared within the enum, are treated as integers, i.e.; each variable of the enumerator can take its corresponding integral value, starting from zero(0). That means in our above discussed example,

  • res = 0
  • green = 1
  • blue = 2

The main purpose of using enumerated variables is to ease the understanding of the program . For example, if we need to employee departments in a payroll program, it’ll make the listing easier to read if we use values like Assembly, Production, Accounts, etc, rather than using 1,2 and 3. Lets take one more example for understanding this

Run
#include<stdio.h>
#include<string.h>
void main()
   {
       enum emp_dept    //declaring enum emp_dept and its variables
           {
                 assembly,
                 manufacturing,
                 accounts,
                 stores
           };
       struct employee   //declaring srtucture
           {
              char name[30];
              int age;
              float bs;
              enum emp_dept department;
           };
         struct employee e;
       strcpy(e.name, "Vibush Upadhyay");   //intializing employee details
       e.age=22;
       e.bs=12500;
       e.department=stores;
printf("\n Name=%s",e.name); printf("\n Age=%d",e.age); printf("\n Basic Salary=%f",e.bs); printf("\n Department=%d",e.department); if(e.department==accounts) printf("\n %s is an accountant",e.name); else printf("\n %s is not an accountant",e.name); }

Output

Name=Vibush Upadhyay
Age=22
Basic Salary=12500.000000
Department=3
Vibush Upadhyay is not an accountant

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

4 comments on “Enums in C”


    • HelpPrepInsta

      Hey Deshna, its simple, since we have taken stores, and we have made e.department = stores, that’s why department is equal to 4


  • Shubham

    In “How does enum works !?” the first point is written wrong it should be “red=0” instead of “res=0”!……


    • HelpPrepInsta

      Hey Shubham, enums are basically used for creating user required datatypes, like we have explained in the example. And yeah thanks for pointing out our silly typing mistake, we’ll surely fix it