4.5 — Enumerated types

C++ allows programmers to create their own data types. Perhaps the simplest method for doing so is via an enumerated type. An enumerated type is a data type where every possible value is defined as a symbolic constant (called an enumerator). Enumerated types are declared via the enum keyword. Let’s look at an example:

Defining an enumerated type does not allocate any memory. When a variable of the enumerated type is declared (such as eColor in the example above), memory is allocated for that variable at that time.

Enum variables are the same size as an int variable. This is because each enumerator is automatically assigned an integer value based on it’s position in the enumeration list. By default, the first enumerator is assigned the integer value 0, and each subsequent enumerator has a value one greater than the previous enumerator:

The cout statement above prints the value 4.

It is possible to explicitly define the value of enumerator. These integer values can be positive or negative and can be non-unique. Any non-defined enumerators are given a value one greater than the previous enumerator.

Because enumerated values evaluate to integers, they can be assigned to integer variables:

However, the compiler will not implicitly cast an integer to an enumerated value. The following will produce a compiler error:

It is possible to use a static_cast to force the compiler to put an integer value into an enumerated type, though it’s generally bad style to do so:

Each enumerated type is considered a distinct type. Consequently, trying to assign enumerators from one enum type to another enum type will cause a compile error:

Enumerated types are incredibly useful for code documentation and readability purposes when you need to represent a specific number of states.

For example, functions often return integers to the caller to represent error codes when something went wrong inside the function. Typically, small negative numbers are used to represent different possible error codes. For example:

However, using magic numbers like this isn’t very descriptive. An alternative method would be through use of an enumerated type:

This is much easier to read and understand than using magic number return values. Furthermore, the caller can test the function’s return value against the appropriate enumerator, which is easier to understand than testing the return result for a specific integer value.

Another use for enums is as array indices, because enumerator indices are more descriptive than integer indices. We will cover this in more detail in the section on arrays.

Finally, as with constant variables, enumerated types show up in the debugger, making them more useful than #defined values in this regard.


1) Define an enumerated type to choose between the following monster types: orcs, goblins, trolls, ogres, and skeletons.

2) Declare a variable of the enumerated type you defined in question 1 and assign it the troll type.

3) True or false. Enumerators can be:
3a) explicitly assigned integer values
3b) not explicitly assigned a value
3c) explicitly assigned floating point values
3d) negative
3e) non-unique
3f) assigned the value of prior enumerators (eg. COLOR_MAGENTA = COLOR_RED)

Quiz answers

1) Show Solution

2) Show Solution

3) Show Solution

4.6 -- Typedefs
4.4a -- Explicit type conversion (casting)

31 comments to 4.5 — Enumerated types

  • tom mason

    When are the enumerators created? Can they be used independently of the enum-type variable? It appears that they are declared as -static const int-

    • Enums are handled at compile time, and they are generally treated as a const int. There is no such thing as a static type, so the static keyword is not applicable. The scope of enum type declarations is the same as normal variables: Enums declared outside of a class or function are treated as global, those inside a block are scoped to that block, and those in a class are considered part of that class.

      For example:

      Enum values can be used independently of the enum-type variable, as they are essentially treated as integers. For example, the following is valid:

      Note that in the specification for the next version of C++ (C++0x), there will be a new type of enum (called a strongly typed enum) that will be treated as a unique type. These strongly typed enums will not be implicitly convertable to integers.

      • Sasan

        Can we define all of our Enums in a separate file (.cpp) and forward declaration them in a Header file and then include that? I tried to do that but compiler gave me error on this (in header file):

        enum Color;

  • Stuart

    Alex, are the semicolons here a mistake?
    My program wouldn’t compile until I changed them to commas:

    • Yup, that was a mistake on my part. Should have been commas. I corrected the example.

      For some reason your comment got caught by my spam filter. I have no idea why though!

      • Stuart

        That’s strange. The first time I commented (a couple of days ago), it went through okay.

        Anyway, thanks for the tutorials. They’re really good. ;)

  • Ben

    Not sure I understand its interest. If you’re trying to get a more understandable error message for example, I don’t see much difference as it will still print an integer and not “ERROR_OPENING_FILE”. Same idea with a list of names, etc…
    Is there a way to print the name of the enum and not its integer value?


    • Unfortunately, there’s no easy way to print the name of the enum. The main benefits of using enums are:
      1) Code readability is enhanced, since enums are descriptions and numbers could mean anything
      2) If you ever want to change an enum’s corresponding integer value, you only have to do so in one place, whereas if you use integers everywhere, you might have to change multiple values.

      Enums are often used in conjunction with functions, either as parameters or return values to denote a particular mode or state.

  • Rousseaux

    i will have to reread this lesson later. But i’m with Ben as far as seeing more use in the name of the enum type being printed in the stead of the actual integer. i tried to find a way to cout the name by feeding the integer value but that skill is beyond my scope so far. Once again you’ve fueled new ‘what if’s’ Alex. Well done.

    • If I need the names of a set of enums for something, I’ll usually write a function that takes the enum name to be printed and return a string version of the enum. Something like this:

      std::string GetEnumName(MyEnum eEnum)
      switch (eEnum)
      case ENUM_CASE_1: return “ENUM_CASE_1″;
      case ENUM_CASE_2: return “ENUM_CASE_2″;
      case ENUM_CASE_3: return “ENUM_CASE_3″;
      case ENUM_CASE_4: return “ENUM_CASE_4″;

      return “”;

  • Kinten

    I dont see the reason for usin enums instead of macros, I mean, if you use the enum it is necesary to initiate at least one instance of it, while the macros (#define) replace everyting right away without the memory cost.
    Isn’t this a better solution?

    • Enums and #defines have the same memory requirements. Enum definitions take up no additional memory space because they are just definitions. A good compiler should replace any enum with it’s integer equivalent at compile time, just like #defines are replaced by the preprocessor.

      Enums are always a better choice than #define when you have a set of options to pick from because the syntax checking will exclude you from picking something not part of the enum set. Plus they’re more readable, and debuggers can resolve their values (whereas debuggers generally can not resolve the values of #defines).

  • Sim

    Is there any other situation, in which enumerators are needed typically? Cause as of now I don’t see any difference between doing this:

    and this:

    Obviously these are constants, thus they cannot be changed during runtime. However, if this is the only typical way of using enumerator types, then I don’t see why you wouldn’t just pre-define them using the preprocessor.

  • Shai

    Great work, Alex.
    Your site is better than any C++ book I know of.

  • John

    I hope that is all in code !

    When I run this, I get the exception in the PrintBodyPartStatus(), which prints 0 0.
    And the check above the exception, is expecting a 0, 0.
    What am I missing? Am I doing this wrong? It seems like a viable way to use Enums, as I’ve been using consts for EVERYTHING until I had a look at some open source game Enums.

    I’m currently working on this, so don’t think I’m commenting to expect you to do it for me (which would be nice, I admit)
    But I really didnt’t see the point in typing out

    where in a few less line I coult just type in

    Thanks in advance

    changing the check from && to || make it work. I’ve always wondered why OR checks work properly and AND checks don’t. (Maybe I’ve been doing those all wrong too, but I’ve always had the problem, even on simple checks.

    even compound if’s don’t work very often


    • Luiwtf

      should be:

      note the 2 =’s, instead of one :)

      the reason it works with || is because the second half comes up true as you have the == correct, as it is the first part is irrelevant.

      edit -- meh, just noticed it’s a month old :D, i suck at remembering dates :p.

  • bla

    I am not really sure about the meaning of questions 3a,b. Does explicit mean explicit cast in this context?

    “3) True or false. Enumerators can be:
    3a) explicitly assigned integer values”
    that would be:


    3b) not explicitly assigned a value

    • sm00th_0perat0r

  • SWEngineer

    Simple well explained tutorial. Thanks.

  • newUser

    If I had not read the comments I would have never noticed enum uses commas! May I suggest having it written in the tutorial too so it draws more attention to it?

  • kalvin

    Enumerations are a little confusing for me. I would better understand them if i had a practical use for them.. I know that no arithmetic operations are allowed on enum, and that since enums are an ordered set of values, you can use relational operators, and loops. this being said I guess I can see how using enum to make your program more readable might be benificial. But to really incorperate enums into your program you will have to input and output enums indirectly as you can not directly output or input enumeration data types.. So for now , i think you have to choose whether to make your program more readable with more work using enumerations, or not and save the extra programming.. Idk maybe as i learn and play with this more i will find its practicality

  • Mindstormscreator

    I’ve seen enums declared without a type before (Animal, Color, etc), are these for defining a bunch of constants essentially?

  • KanedaSyndrome

    This was an area that I haven’t worked with before, so it was fun to get into.

    My contribution:


    enum monsters

    declaraction and assignment

    monsters newMonster = TROLL;


    cout << endl << newMonster; //outputs the value 2


  • Jannat

    Hi. I found these enumerated types really interesting. So I tried to make this program.
    I keep on getting the following error.

    error: no match for ‘operator>>’ in ‘std::cin >> eColour’|

    using namespace std;
    int main ()
    { //program to input a colour and output the lucky number. lol
    enum colour
    red = 1,
    blue = 2,
    green = 3,
    colour eColour = red;
    cout<<"Choose a colour "<>eColour ;
    cout <<" Your lucky number is "<<eColour<<endl;
    return 0;

    What's wrong with cin ? can these enumerated data types not be input?

  • dice3000

    Awesome tutorial. I actually learned something really useful

  • JesseZhuang

    Is non-unique enumerators useful in any case in practice?

  • Subhan

    Can’t I set enumerated values to be greater than int values? Example

  • Joseph

    Should the last enumerator variable have a comma after it?
    Wouldn’t the compiler expect another variable to follow and give an error? Mine doesn’t so I don’t know if this is okay or not.

    enum MonsterType

  • Jeremy

    I’m having a heck of a time trying to figure out how to forward declare enumerated classes/structs (same thing, I know). I know you haven’t touched on scoped and unscoped enums here yet, but you probably will in the future. I think they’re cool and all, but I can’t figure out how to make a forward declaration work for them. It seems easier to just stuff the whole definition in a header file and include it to avoid the headache, but surely forward declarations for enums (and structs for that matter) exist for a reason?

  • christopher11

    This is what i came up with
    using namespace std;

    enum Race
    RACE_ORC = 1,
    RACE_GOBLIN = 2,
    RACE_TROLL = 3,
    RACE_OGRE = 4

    int pickRace(int race);

    int x;
    cout<< "Choose your race: " << endl << "Orc(1) -- Goblin(2) -- Troll(3) -- Ogre(4)";
    cout<> x;
    return 0;

    int pickRace(int race)
    static int choice;
    choice = RACE_ORC;
    choice = RACE_GOBLIN;
    choice = RACE_TROLL;
    choice = RACE_OGRE;

    cout<<"You picked Orc.";
    cout<<"You picked Goblin.";
    cout<<"You picked Troll.";
    cout<<"You picked Ogre.";
    cout<< endl;

  • PLEASE avoid using system(“PAUSE”). use cin.get() or something but, please avoid using any system(“”) function. That way you’ll be doing favors both for yourself AND others who’re reading your code.

Leave a Comment




8 − six =

Put C++ code inside [code][/code] brackets to use the syntax highlighter