The Mysterious enum Keyword

They’re the orphan keywords, urchins, unwanted, unused, unloved. Of the 32 C language keywords, a handful are seldom used. These include: auto, enum, register, union, and volatile.

A few others are used sparingly, such as const and static. I won’t even get into the underlined keywords added with the C11 standard.

Of all the orphans, the one I keep coming back to is enum. For some reason it piques the curiosity of many programmers, wondering what the heck it actually does.

In my code, I’ve used enum once. It provided an elegant solution to a simple problem and helped make the code more readable.

The word enum is short for enumerate, which is the process of assigning numbers to something or listing out specific items. In the C language, enum creates a list of constants, each of which is assigned a value used elsewhere in the code.

What enum doesn’t do is to create an associative array. It doesn’t create an array at all. It creates constants, similar to the define directive. Unlike define, enum builds a slew of constants at once, each numbered sequentially. Here’s the format:

enum {const1, const2, ...};

The enum keyword creates constants const1, through whatever. The format is similar to an array, but the items listed become constants. I typically use ALL CAPS for the names, but that’s not a rule.

Values assigned to the constants start with 0 and increment by 1. The following enum statement is popular in a few programs:

enum {FALSE,TRUE};

This statement sets the value of constant FALSE to zero and TRUE to one. Here’s sample code:

#include <stdio.h>

int main()
{
    enum {FALSE,TRUE};

    printf("FALSE is %d\n",FALSE);
    printf("TRUE is %d\n",TRUE);

    return(0);
}

Here’s the output:

FALSE is 0
TRUE is 1

The effect is the same as if I’d used:

#define FALSE 0
#define TRUE 1

One major difference between enum and define is where you find these statements in the code. I typically use define externally so that the constants are available throughout the code. You can use enum that way, but when you place it inside a function, the constants are available only inside that function.

Unless you specify otherwise, enum constants start at value 0 and increase by 1 for each constant. You can set new values by using the = operator. For example:

enum {ALPHA=1,BETA,GAMMA};

Above, ALPHA is set to value 1, then BETA would be 2 and GAMMA 3. Or you could do this:

enum {ALPHA=1,BETA=5,GAMMA};

Above, ALPHA is set to value 1, then BETA is set to 5. GAMMA would be equal to 6.

Of course, if you’re going to set individual constant values, just use define instead of enum.

Here’s another example:

#include <stdio.h>

int main()
{
    enum {ALPHA=1,BETA,GAMMA};
    int test;

    printf("Which test is this? (1-3): ");
    scanf("%d",&test);

    switch(test)
    {
        case ALPHA:
            puts("Alpha test");
            break;
        case BETA:
            puts("Beta test");
            break;
        case GAMMA:
            puts("Gamma test/release");
            break;
        default:
            puts("Unknown test number");
    }

    return(0);
}

Because enum sets the constant ALPHA to 1, the test variable set at Line 9 matches the enumerated constant values.

If you can use enum in your code, great. You’ll join the ranks of those few C coders who understand and appreciate how it works. Otherwise, it ain’t no big deal.

Leave a Reply