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");
}