Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of enums in C and C++

Tags:

c++

c

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.

like image 580
kadina Avatar asked Mar 25 '14 21:03

kadina


People also ask

What is the use 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.

What is the advantage of enum in C?

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.

What is the type of enum in C?

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.

What is the scope of enum in C?

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.


3 Answers

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.

like image 136
Christos Avatar answered Oct 11 '22 20:10

Christos


Advantages over macros

  • Debuggers can print names for values
  • Your constants will be scoped (if you want, you can put them also in a namespace in C++)
  • The compiler can warn if you forget an enum constant in a switch
  • Values for the constants are automatically assigned, if you don't give explicit values
  • The enum type is always large enough to hold all the constants. When using #define, you have to commit to int or some typedef and ensure that all the constants fit manually
like image 30
Johannes Schaub - litb Avatar answered Oct 11 '22 21:10

Johannes Schaub - litb


An 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)

like image 3
Excelcius Avatar answered Oct 11 '22 22:10

Excelcius