Enum is list of constant integer values. It can be used instead of defining constant values using #define. But other than that, I haven't found any substantial uses of enums in C and CPP. Can any one please let me know what are the exact uses of enums.
Initially I thought if we create a enum variable and assign a value which is not in the enum values, the compiler will shout. But that is not true. We can assign any value to the enum variable. I can't think of any substantial uses of enum.
An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.
The benefits of using enumerations include: Reduces errors caused by transposing or mistyping numbers. Makes it easy to change values in the future. Makes code easier to read, which means it is less likely that errors will creep into it.
In C programming, an enumeration type (also called enum) is a data type that consists of integral constants. To define enums, the enum keyword is used. enum flag {const1, const2, ..., constN}; By default, const1 is 0, const2 is 1 and so on.
In C, there is simply no rule for scope for enums and struct. The place where you define your enum doesn't have any importance. In C++, define something inside another something (like an enum in a class) make this something belong to the another something.
Enums are very useful in programming, because they make your code both more readable and more consistent.
For instance: say that we have the following switch statement:
switch(fruit)
{
case 1:
// code goes here
break;
case 2:
// code goes here
break;
case 3:
// code goes here
break;
default:
// code goes here
break;
}
It would be far more clear, if you had define an enum fruit that would contain the fruits you have and then use it the following way:
switch(fruit)
{
case fruits::apple:
// code goes here
break;
case fruits::orange:
// code goes here
break;
case fruits::banana:
// code goes here
break;
default:
// code goes here
break;
}
where fruits
is an enum.
enum fruits { apple, orange, banana };
Compare now the first snippet to the second one. The second is more readable and in addition to that you don't have to remember that 1 is for apple, 2 for banana and so on.
Advantages over macros
#define
, you have to commit to int
or some typedef and ensure that all the constants fit manuallyAn enum isn't that much different from a #define
, in addition to what others have already said, the main difference to me is this:
You can group constants together
Also, an enum has an underlying type which can be specified to your needs. In C++11 you can use enum class
to get a strong typed and scoped enum which makes enums even more useful.
In general, try to avoid the preprocessor as much as you can as it doesn't have any idea about the code and what it's trying to do. Instead of using a define for a single constant, consider writing:
const int MY_CONSTANT = 42;
(and all its static, member or namespace variants of defining a typed constant)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With