0

What's the advantage of creating a variable of enum type? I know how to define an enum type, and a variable - however, I don't quite see the advantage?

#include <stdio.h>
int main(void){

    enum colour{
        white,
        red=2,
        green,
        blue,
        yellow,
        black
    } jacket;

    jacket=50;
    int shirt=yellow;
    printf("%d\n",jacket);
    printf("%d",shirt);

   return 0;
}

In the above code, jacket is defined as enum but shirt is defined as an integer. The enum type doesn't prevent me from using a value that isn't specified as an element. Nor does the integer type prevent me from assigning it to a value from the enum type.

The above code gives the following output:

50
5

What's the functionaly difference, if any, between these variables?

  • Just for the record, Java, C++, and C# all have more advanced uses of enum, where the advantages are even more clearly delimited. – Andrew Lazarus Aug 29 '14 at 17:37

4 Answers4

3

What's the functionaly difference, if any, between these variables?

shirt and jacket are both of integer types but shirt is int and is enum colour. The latter is an implementation defined integer type and can be unsigned int for example.

In gcc for example, by default enum colour is unsigned int.

Now using variable of enum type has some advantage. For example, some compilers are able to detect and warn the mix-up of different enum types. Also if you use an object of an enum type as the controlling expression of a switch statement, some compilers (e.g., gcc with -Wswitch) are able to warn if you don't list all enum constant cases. Also debuggers are usually able to display some nice symbols for enum objects values.

ouah
  • 134,166
  • 14
  • 247
  • 314
2

An enum is just a way of giving constants symbolic values; the constants are just integers' There are a couple of advantages to this, compared to defines: The values, if consecutive, are automatically created, and an enum obeys block scope. It also tells other programmers that you except the variable to only take on those values. A debugger can show the symbolic names, which can be helpful instead of just showing the raw values.

Frank H.
  • 1,055
  • 1
  • 9
  • 22
  • Add to that the fact that it makes code maintainability much easier. Let's say that "jacket" should be changed to be represented by 51, instead of 50. This will have to be changed in all places in code that need to compare against jacket. On the other hand, if you were to add a value to your enum above "yellow", it will change the __value__ of yellow, but your __usage__ will not need to change at all – Don Shankin Aug 29 '14 at 17:59
1

Which code is more easily understandable?

Tshirt tee = new Tshirt();

tee.setColor(4);

or

enum color { red, green, blue, black, pink, grey, brown };
Tshirt *tee = new Tshirt();

tee.setColor( pink );
CarCzar
  • 140
  • 12
0

One of the advantages is, it increases program readability.

For example:

enum colour{white, red=2, green, blue, yellow, black} jacket;

int shirt_price[10];

shirt_price[red] = 50;
shirt_price[green] = 100;
shirt_price[black] = 500;

Now you don't need to remember the indexes here just use enum variable.

ani627
  • 4,851
  • 8
  • 33
  • 42