Computer Programming C: UNIT IV: Structures and Unions

Enumerations

C Programming

1. Enumeration (or enum) in C 2. Create enum Variables 3. Initialization 4. Assign Values Manually 5. Size of Enums 6. Enum and Typedef 7. Applications of Enums in C 8. Enum in Switch Statements

ENUMERATIONS

 

1. Enumeration (or enum) in C

2. Create enum Variables

3. Initialization

4. Assign Values Manually

5. Size of Enums

6. Enum and Typedef

7. Applications of Enums in C

8. Enum in Switch Statements

 

1. Enumeration (or enum) in C

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.

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

enum enum name

{

       al, a2, a3,…

};

Where, a1, a2, a3, … are names assigned to an integer value. By default, first name al is assigned 0, a2 is assigned 1 and the subsequent ones are incremented by 1.

Example:

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.

enum calculate

{

sum, differeNCE, PRODUCT, QUOTIENT

};

enum item

{

PRODUCT, SERVICE

};

 

2. Create enum Variables

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

enum_name v;

 

3. Initialization

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

#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.

 

4. Assign Values Manually

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

enum_enum_name

{

        nl = vall, 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.

#include <stdio.h>

// Defining enum

enum enm

{

      a=3, b = 2, c

};

int main()

{.

        // Creating enum variable

        enum enm vl = 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.

 

5. 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.

#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

 

6. 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.

#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.

 

7. Applications of Enums in Cus

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.

 

8. Enum in Switch Statements

C language switch case statement works with integral values. We can use enum type to define constants with (or, without) integral type values to use them in switch case statements.

Example: The colors in the rainbow are enumerated.

#include <stdio.h>

// Enum declaration

enum colors.

{

      VIOLET, INDIGO, BLUE, GREEN, YELLOW, ORANGE, RED

};

int main()

{

       // Enum variable declaration enum colors color = YELLOW;

       // switch statement using enum

switch (color)

{

        case BLUE:

                printf("Blue color");

                break;

        case GREEN:

                printf("Green color");

                break;

        case RED:

                printf("Red color");

                break;

        default:

                printf("Color other than RGB");

return 0;

}

OUTPUT

Color other than RGB

 

Computer Programming C: UNIT IV: Structures and Unions : Tag: Computer Science : C Programming - Enumerations


Computer Programming C: UNIT IV: Structures and Unions



Under Subject


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation



Related Subjects


English Essentials I

EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


தமிழர் மரபு - Heritage of Tamils

UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Calculus

MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I

PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Chemistry I

CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Makerspace

ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming Python

CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Fundamentals of Electrical and Electronics Engineering

EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Mechanical Engineering

ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Civil Engineering

CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing

CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I Laboratory

PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Applied Chemistry I Laboratory

CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming Python Laboratory

CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Engineering Drawing

ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electronics and Electrical Engineering

EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing - Laboratory

CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation