1. Technology

Enum Variables in C

Using Enums

By

This is the third tutorial in the series Online C Tutorial for beginners and is about enum variables. The Online C Tutorial.

This is a rewriting of the original C tutorials into single page bite size tutorials which can usually be run on either codepad.org or ideone.com.

Note these online services and others are provided free by third party providers so please don't abuse them. I've used them because they save you the hassle of installing a C compiler. You might consider doing that and running them instead because with many users hitting these services the response may get slow.

What are Enums?

Enums are an oddity in C, coming in as not much better than syntatic sugar; i.e. to make your program easier to read.

An enum variable is just an int but the values it holds are given names in your program- these are the enum values. In Pascal and C# for example, you can only assign to an enum variable the values you have defined in the enum type declaration. In C, you can assign any int so it's not type safe.

You could avoid enums by using #defines instead but (a) you'd clutter it up and (b) it's better through C's scope rules.

Here's an example:

#include <stdio.h>

enum monster {orc,dwarf,goblin,kobold,balrog};

int main(int argc, char* argv[])
{
    enum monster attacker = dwarf;
    enum monster defender = orc;
    printf("attacker = %d defender = %d",attacker,defender) ;
    return 0;
}

The enum type monster holds values that are coded as orc, dwarf, goblin etc. Note this line just declares the type monster.

When this is run, it outputs:

attacker = 1 defender = 0

Run it online this Ideone.com link.

You can add a variable on the end of the declaration though personally I consider it bad practice; type declarations and variable declarations should be kept separate.

But in case you ever see it, this is what it would look like if we declared a variable mindex in the monster type declaration.

enum monster {orc,dwarf,goblin,kobold,balrog} mindex;

Enums are Int Values

In case you haven't guessed it, the compiler allocates int values starting with 0 to each member of the enum. So orc being first is 0, dwarf is 1 ... and balrog is 4.

You can specify any value by adding = value after a enum member. So if we add ,human=128,woman after balrog then human takes the value 128 and woman is one up from that at 129 as this short example shows.

#include <stdio.h>

enum monster {orc,dwarf,goblin,kobold,balrog,human=128,woman} mindex=woman;

int main(int argc, char* argv[])
{
    printf("man = %d woman = %d",human,mindex) ;
    return 0;
}

Run that and you get:

man = 128 woman = 129

Of note here is that (a) the mindex value was declared and initialized in the enum statement and (b) printf included both the variable mindex and a member value.

Using Enums

You could do without them and use #define instead but defines are global to the whole file, whereas enums follow C's scope rules so by declaring it and using it completely within a function, it can't be accessed from outside that function.

Any declaration of an enum type variable needs the word enum to prefix the type name. You can't do this using the type monster from the examples above:

monster g = goblin;

That generates a compiler error, instead you need the prefix enum like this:

enum monster g = goblin;

You'll also see this with structs when we come to them in a couple of tutorials. Now having to add this prefix to every enum or struct declaration could be a real pain, but thankfully C has provided a way round this called typedef.

There's also another way using a #define but typedef is better so I'm not showing the #define way.

Using typedef

This lets you create an alias for another type. It can be used to simplify type declarations; C is very good at letting you create write only declarations. They made sense when you wrote them but two months later you haven't a clue when you read them!

Instead of declaring enum monster variables, follow the original type declaration with a typedef like this:

enum monster {orc,dwarf,goblin,kobold,balrog,human=128,woman} mindex=woman;
typedef enum monster monstertype;

Now instead of this:

enum monster g = goblin;

Use this instead:

monstertype g = goblin;

And just to prove it, run this:

#include <stdio.h>

enum monster {orc,dwarf,goblin,kobold,balrog,human=128,woman} mindex=woman;
typedef enum monster monstertype;

int main(int argc, char* argv[])
{
    monstertype m= goblin;
    printf("goblin = %d",m) ;
    return 0;
}

Which outputs:

goblin = 2

That's it for enums. In the next tutorial I'll look at arrays.

  1. About.com
  2. Technology
  3. C / C++ / C#
  4. C Programming
  5. C Tutorials
  6. Online C Programming Tutorial for Beginners - Lesson Three - Enum Variables

©2014 About.com. All rights reserved.