enum flag{integer_const1, integer_const2,.....integter_constN};
In this declaration, we are specifying an enum called flag that includes 'N' integer constants. By default, integerconst1 is set to 0, integerconst2 is set to 1, and so forth. It is also possible to modify the default values of these integer constants during the declaration process.
For example:
enum fruits{mango, apple, strawberry, papaya};
The initial value assigned to mango is 0, apple is 1, strawberry is 2, and papaya is 3. To modify these default assignments, follow the steps provided:
enum fruits{
mango=2,
apple=1,
strawberry=5,
papaya=7,
};
Enumerated type declaration
In C programming, it is a requirement to define variables using built-in types like int, float, and char. In the same manner, it is possible to define variables using custom data types like enum. Now, let's explore the process of declaring a variable with an enum data type.
Suppose we define an enumeration named status with the following syntax:
enum status{false,true};
Now, we create the variable of status type:
enum status s; // creating a variable of the status type.
In the previous statement, we have defined the variable 's' as a status type.
To define a variable, you can express the preceding two statements in code as:
enum status{false,true} s;
In this scenario, the initial value of false will correspond to 0, while the value of true will correspond to 1.
Let's create a simple program of enum.
#include <stdio.h>
enum weekdays{Sunday=1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
int main()
{
enum weekdays w; // variable declaration of weekdays type
w=Monday; // assigning value of Monday to w.
printf("The value of w is %d",w);
return 0;
}
In the provided code snippet, we define an enumeration called "weekdays" that includes the names of the seven days of the week. Sunday is explicitly set to a value of 1, while the subsequent days are assigned values incrementing by one from the previous day.
Output
The value of w is 2
Let's illustrate an additional example to gain a clearer understanding of the enum.
#include <stdio.h>
enum months{jan=1, feb, march, april, may, june, july, august, september, october, november, december};
int main()
{
// printing the values of months
for(int i=jan;i<=december;i++)
{
printf("%d, ",i);
}
return 0;
}
In the provided code snippet, a new enum type called "months" has been defined to hold the names of all twelve months. The initial value of '1' has been assigned to the first month, with subsequent months being assigned values one greater than the previous one. Within the main function, a for loop has been implemented starting from January ('jan') and running until December.
Output
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
Enums are employed when restricting a variable to a specific set of values is necessary. For instance, consider defining a variable to represent directions. With the cardinal directions of North, South, East, and West, this variable can only be assigned one of these four values at a time. Any attempt to assign a value outside of this predefined set will result in a compilation error.
The enum type is also employed within a switch-case statement by passing the enum variable within the switch parentheses. This guarantees that the case block's value must be specified within an enum.
Let's explore the utilization of an enumeration in a switch-case construct.
#include <stdio.h>
enum days{sunday=1, monday, tuesday, wednesday, thursday, friday, saturday};
int main()
{
enum days d;
d=monday;
switch(d)
{
case sunday:
printf("Today is sunday");
break;
case monday:
printf("Today is monday");
break;
case tuesday:
printf("Today is tuesday");
break;
case wednesday:
printf("Today is wednesday");
break;
case thursday:
printf("Today is thursday");
break;
case friday:
printf("Today is friday");
break;
case saturday:
printf("Today is saturday");
break;
}
return 0;
}
Output
Today is monday
Some key aspects concerning enum:
- Enum names within an enum type can share the same value. Let's illustrate this with an example.
#include <stdio.h>
int main(void) {
enum fruits{mango = 1, strawberry=0, apple=1};
printf("The value of mango is %d", mango);
printf("\nThe value of apple is %d", apple);
return 0;
}
Output
The value of mango is 1
The value of apple is 1
- If we do not provide any value to the enum names, then the compiler will automatically assign the default values to the enum names starting from 0.
- We can also provide the values to the enum name in any order, and the unassigned names will get the default value as the previous one plus one.
- The values assigned to the enum names must be integral constant, i.e., it should not be of other types such string, float, etc.
- All the enum names must be unique in their scope, i.e., if we define two enum having same scope, then these two enums should have different enum names otherwise compiler will throw an error.
Let's grasp this situation with an illustration.
#include <stdio.h>
enum status{success, fail};
enum boolen{fail,pass};
int main(void) {
printf("The value of success is %d", success);
return 0;
}
Output
In enumeration, it is possible to specify an enumerated data type even without naming it explicitly.
#include <stdio.h>
enum {success, fail} status;
int main(void) {
status=success;
printf("The value of status is %d", status);
return 0;
}
Output
The value of status is 0
Enum vs. Macro in C
- Macro can also be used to define the name constants, but in case of an enum, all the name constants can be grouped together in a single statement. For example, # define pass 0; # define success 1; The above two statements can be written in a single statement by using the enum type. enum status{pass, success};
- The enum type follows the scope rules while macro does not follow the scope rules.
- In Enum, if we do not assign the values to the enum names, then the compiler will automatically assign the default value to the enum names. But, in the case of macro, the values need to be explicitly assigned.
- The type of enum in C is an integer, but the type of macro can be of any type.