C Programming • Structure and Union
C Programming / Enumerated data types

Enumerated data types

Notes 4 Structure and Union

An enumerated data type, or enum, is a user-defined data type in C that allows a programmer to create a set of named integer constants. Instead of using numbers directly, meaningful names can be used to represent related values.

Notes

An enumerated data type, or enum, is a user-defined data type in C that allows a programmer to create a set of named integer constants. Instead of using numbers directly, meaningful names can be used to represent related values.

💡 Example
enum Status { single, married, divorced, widowed };

Here, single, married, divorced and widowed are named enumeration constants.

Why Use enum?

An enum makes a program easier to understand because meaningful names can be used instead of unexplained integer values. It is useful when a variable can have one value from a fixed set of related choices.

💡 Example

Instead of storing marital status as 0, 1, 2 and 3, we can use single, married, divorced and widowed.

Syntax of enum

The enum keyword is followed by the enumeration name and a list of named constants enclosed in braces.

enum enum_name { constant1, constant2, constant3 };
💡 Example
enum Day { Monday, Tuesday, Wednesday, Thursday, Friday };

Declaring an enum Variable

After defining an enumeration type, a variable of that enum type can be declared. The variable can then store one of the defined enumeration values.

💡 Example
enum Day today;

Here, today is an enum variable of type Day.

Assigning Values to enum Constants

By default, enumeration constants are assigned integer values starting from 0 and increasing by 1. The programmer can also explicitly assign values when required.

💡 Example
enum Status { single = 100, married = 200, divorced = 300, widowed = 400 };

Here, the enumeration constants are assigned explicit integer values instead of the default sequence.

Using enum in a Program

An enum variable can be assigned one of the named constants. Using these names makes the purpose of the value clear to the reader of the program.

💡 Example
enum Status status; status = married;

The variable status is assigned the enumeration constant married.

Default Integer Values

When values are not explicitly specified, the first enumeration constant gets the value 0, the next gets 1, and so on. These are integer values internally, but the named constants make the program easier to read.

💡 Example
enum Color { Red, Green, Blue };

The constants are assigned values in sequence: Red = 0, Green = 1 and Blue = 2.

Advantages of enum

Advantage Explanation
Readability Meaningful names are easier to understand than raw numbers.
Better Organization Related constant values can be grouped together.
Fixed Set of Values Useful when a variable should represent one choice from a known set.
Maintainability Named constants make programs easier to read and maintain.

Practical Example

Problem Statement

Write a C program using an enumerated data type to represent the status of an employee and display the selected status.

Learning Outcomes

  • Define an enumerated data type.
  • Declare and use an enum variable.
  • Understand named integer constants.

Hint

Create an enum named Status containing single, married, divorced and widowed. Assign one of these values to an enum variable.

Theory

An enumeration provides names for a related set of integer constants. It is particularly useful when a variable can have one value from a small, predefined set of choices.

Program

#include <stdio.h> enum Status { single, married, divorced, widowed }; int main() { enum Status employeeStatus; employeeStatus = married; // display the selected employee status if (employeeStatus == married) printf("Employee Status = Married "); return 0; }

Expected Output

Employee Status = Married

Note

In this program, married is one of the named constants defined inside the enumeration. Internally, enumeration constants are represented as integer values, but their names make the program easier to understand.

enum with switch

Enumeration values are particularly useful with a switch statement when different actions need to be performed for different named choices.

💡 Example
switch (status) { case single: printf("Single"); break; case married: printf("Married"); break; }

Common Mistake

Enumeration constants are names representing integer values. They should not be confused with strings such as "married". The enum constant married is not a string.

⚠️ Remember

married → enumeration constant
"married" → string literal

Quick Revision

Concept Remember
enum User-defined type containing named integer constants.
Keyword enum
Default Values Start from 0 and increase by 1 unless explicitly assigned.
Use Represents one choice from a fixed set of related values.
Example enum Status { single, married, divorced, widowed };

Important Exam Questions

Short Answer Questions

  1. What is an enumerated data type?
  2. What is the purpose of the enum keyword?
  3. What are enumeration constants?
  4. What values are assigned by default to enum constants?
  5. Can specific values be assigned to enumeration constants?
  6. Why is enum useful for program readability?

Long Answer Questions

  1. Explain enumerated data types in C with syntax and suitable example.
  2. Explain the advantages and uses of enumerated data types.
  3. Write a C program to demonstrate the use of an enum variable.
  4. Explain the default and user-defined values of enumeration constants.
🎥 Recommended Learning

Watch a beginner-friendly explanation of enumerated data types in C programming.

▶ Watch: enum in C — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for enumerated data types will be provided here.

🧠 Mind Map

Use the mind map for quick revision of enum, constants, default values and practical uses.