Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
335 views
in C Programming by (106k points)
Learn about C enumeration (enum) and its usage in programming. Understand how to define and use enums in C, including assigning values and accessing enum members. Discover the benefits of using enums for improved code readability and maintenance. Explore real-world examples and best practices for utilizing enums effectively in C programming.

Please log in or register to answer this question.

2 Answers

0 votes
by (106k points)

C Enumerations

Enumerations, also known as enums, are a way to define a set of named integer constants in C. They provide a convenient and readable way to represent a fixed set of values that an identifier can take. Enums make the code more expressive and maintainable by giving meaningful names to integral values.

Syntax of C Enumerations

The syntax for defining an enum in C is as follows:

enum enum_name {
    constant1,
    constant2,
    constant3,
    // ...
};
 

Here, enum_name is the name of the enum, and constant1, constant2, constant3, and so on are the named constants (also called enumerators) associated with the enum.

Steps to Use C Enumerations

To use C enumerations in your code, follow these steps:

Step 1: Declare the Enumeration

First, you need to declare the enumeration by defining its name and the set of constants it can take. This is typically done outside of any function, at the global scope.

enum Weekday {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
};
 

In this example, we have defined an enum named Weekday with seven constants representing the days of the week.

Step 2: Declare Variables

Next, you can declare variables of the enumeration type. These variables can hold one of the defined constants.

enum Weekday today;
enum Weekday tomorrow;
 

Here, we have declared two variables today and tomorrow of type enum Weekday.

Step 3: Assign Values

You can assign values to enum variables using the defined constants.

today = MONDAY;
tomorrow = TUESDAY;
 

In this case, we assigned the values MONDAY and TUESDAY to the variables today and tomorrow, respectively.

Step 4: Using Enum Variables

You can use enum variables in your code to represent and manipulate the corresponding values. 

For example:

if (today == MONDAY) {
    printf("Today is Monday!\n");
} else {
    printf("Today is not Monday.\n");
}

switch (tomorrow) {
    case TUESDAY:
        printf("Tomorrow is Tuesday!\n");
        break;
    case WEDNESDAY:
        printf("Tomorrow is Wednesday!\n");
        break;
    default:
        printf("Tomorrow is another day.\n");
        break;
}
 

In this code snippet, we demonstrate how to compare enum variables with constants and use them in control flow statements.

Complete Example Code

Here's a complete example that demonstrates the usage of C enumerations:

#include <stdio.h>

enum Weekday {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
};

int main() {
    enum Weekday today;
    enum Weekday tomorrow;

    today = MONDAY;
    tomorrow = TUESDAY;

    if (today == MONDAY) {
        printf("Today is Monday!\n");
    } else {
        printf("Today is not Monday.\n");
    }

    switch (tomorrow) {
        case TUESDAY:
            printf("Tomorrow is Tuesday!\n");
            break;
        case WEDNESDAY:
            printf("Tomorrow is Wednesday!\n");
            break;
        default:
            printf("Tomorrow is another day.\n");
            break;
    }

    return 0;
}
 

When you run this code, it will output:

Today is Monday!
Tomorrow is Tuesday!
 

That's it! You have now learned how to define and use C enumerations (enums) in your code. Enums are useful for creating more readable and maintainable programs by providing meaningful names to integral values.

0 votes
by (106k points)

FAQs on C Enumeration (enum)

Q: What is an enumeration (enum) in C?

A: In C, an enumeration, also known as enum, is a user-defined data type that consists of a set of named constants called enumerators. These enumerators represent a finite list of values that the variable of the enum type can take. Each enumerator is assigned an integer value, which can be explicitly specified or automatically assigned by the compiler.

Q: How do you define an enumeration in C?

A: Enumerations are defined using the enum keyword followed by the name of the enumeration and a list of enumerators enclosed in curly braces. 

Here's the syntax:

enum enum_name {
   enumerator1,
   enumerator2,
   // ...
};
 

Q: Can you provide an example of defining an enumeration?

A: Certainly! Here's an example that defines an enum named "Weekdays" representing the days of the week:

#include <stdio.h>

enum Weekdays {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
};

int main() {
    enum Weekdays today = WEDNESDAY;
    printf("Today is %d\n", today);
    return 0;
}
 

Q: How can I assign specific values to the enumerators?

A: By default, if no values are assigned explicitly, the enumerators are assigned sequential integer values starting from 0. However, you can assign specific values to the enumerators using the assignment operator (=). If an enumerator is assigned a value, the subsequent enumerators will have values incremented by 1.

Here's an example that assigns specific values to the Weekdays enumeration:

enum Weekdays {
    MONDAY = 1,
    TUESDAY,
    WEDNESDAY = 5,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
};
 

Q: How can I declare variables of an enum type?

A: Variables of an enum type can be declared using the enum keyword followed by the enum name and the variable name. 

Here's an example:

enum Weekdays {
    MONDAY,
    TUESDAY,
    // ...
};

enum Weekdays today;
 

You can also combine the declaration and initialization in a single line:

enum Weekdays {
    MONDAY,
    TUESDAY,
    // ...
};

enum Weekdays today = TUESDAY;
 

Q: How can I access the values of enumerators?

A: The values of enumerators can be accessed using the dot operator (.) with the enum variable name followed by the enumerator name. 

Here's an example:

enum Weekdays {
    MONDAY,
    TUESDAY,
    // ...
};

enum Weekdays today = TUESDAY;
printf("Today is %d\n", today);
 

Q: Can enum types be used as function parameters and return types?

A: Yes, enum types can be used as function parameters and return types. 

Here's an example:

enum Weekdays {
    MONDAY,
    TUESDAY,
    // ...
};

enum Weekdays getNextDay(enum Weekdays currentDay) {
    return (currentDay + 1) % 7;
}
 

Q: Can I use typedef with enums?

A: Yes, you can use typedef to define a new type name for an enum. This can make your code more readable and provide a level of abstraction. 

Here's an example:

typedef enum {
    SMALL,
    MEDIUM,
    LARGE
} Size;

Size mySize = MEDIUM;
 

This allows you to use Size as a type instead of writing enum each time.

Important Interview Questions and Answers on C Enumeration (enum)

Q: What is an enumeration (enum) in C? 

An enumeration is a user-defined data type in C that consists of a set of named constants. It allows you to define a group of related constants that can be assigned to variables. Enumerations make the code more readable and maintainable.

Q: How do you declare an enumeration in C? 

An enumeration in C is declared using the enum keyword. 

Here's an example:

enum Month {
  JANUARY,
  FEBRUARY,
  MARCH,
  APRIL,
  MAY,
  JUNE,
  JULY,
  AUGUST,
  SEPTEMBER,
  OCTOBER,
  NOVEMBER,
  DECEMBER
};
 

Q: How do you define variables of an enumeration type? 

Variables of an enumeration type are defined by specifying the enumeration name followed by the variable name. 

Here's an example:

enum Month currentMonth;
 

Q: Can you assign values to enum constants? 

Yes, you can assign values to enum constants explicitly. By default, the first constant is assigned 0, and each subsequent constant is assigned one greater than the previous constant. However, you can explicitly assign values as needed. 

Here's an example:

enum Month {
  JANUARY = 1,
  FEBRUARY = 3,
  MARCH = 6,
  APRIL = 9,
  MAY = 12
};
 

Q: How do you assign a value to an enum variable? 

You can assign a value to an enum variable using the assignment operator (=). The assigned value must be one of the constants defined in the enumeration. 

Here's an example:

enum Month currentMonth;
currentMonth = APRIL; 

Q: How do you iterate over an enumeration in C? 

To iterate over an enumeration, you can use a for loop. Here's an example that prints all the months:

enum Month {
  JANUARY,
  FEBRUARY,
  MARCH,
  APRIL,
  MAY,
  JUNE,
  JULY,
  AUGUST,
  SEPTEMBER,
  OCTOBER,
  NOVEMBER,
  DECEMBER
};

int main() {
  enum Month currentMonth;

  for (currentMonth = JANUARY; currentMonth <= DECEMBER; currentMonth++) {
    printf("Month: %d\n", currentMonth);
  }

  return 0;
}
 

Q: Can enum constants be used as case labels in a switch statement? 

Yes, enum constants can be used as case labels in a switch statement. 

Here's an example:

enum Day {
  MONDAY,
  TUESDAY,
  WEDNESDAY,
  THURSDAY,
  FRIDAY,
  SATURDAY,
  SUNDAY
};

enum Day today = FRIDAY;

switch (today) {
  case MONDAY:
    printf("It's Monday.\n");
    break;
  case TUESDAY:
    printf("It's Tuesday.\n");
    break;
  // Handle other days...
  default:
    printf("It's not a weekday.\n");
}

Related questions

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...