Tech Today

What exactly does enum do in [C]?

Posted on: May 23, 2009

Ever wonder what the word enum means in [C]? I know most of us don’t spend the majority of our days pondering over its definition but still I thought I would add a simple tutorial to my repertoire depicting its uses.

So basically, enum creates a list of aliases. What are aliases? Well an alias is something that assumes the identity of something else. Think about your favourite spy assuming the alias of a henchmen to infiltrate the enemy base. The spy in this case assumed the identity of someone else. Got it? Great!

When we say:

enum person

You are getting ready to list a bunch of aliases for a person. Like so:

enum person {henchmen, guard, janitor};

Next we declare a variable for our person called spy and now aliases for a spy are a henchmen, guard, or janitor.

person spy;

The spy just isn’t going to use the same alias all the time, he may want to switch one out for another. So to apply an alias to a spy we write:

spy = henchmen;

Now the spy assumes the identity of the henchmen.

If we were to print out the value of the spy to the command line we would see that the value of a spy is 0. Consequently if we were to assign the spy the alias of the guard he we have a value of 1 and finally if he were to assume the janitor alias he would have a value of 2. Now thats kool right?! By not defining an initial value for a henchmen you are saying that you want a henchmen to be equal to zero and every alias after that to be one plus the previous number. If you decide to define the first value like so:

enum person {henchmen=5, guard, janitor};

Then henchmen will be 5, guard 6, and janitor 7.

Okay, now that you understand what enum does lets drive it home with an interactive program written using one. This simple program uses the months of the year defined 1 – 12. The user inputs the number for a month and the program uses aliases to print out which month was inputed.

CODE:

#include <stdio.h>
#include <stdlib.h>

main(int argc, char *argv[]) {
 /*
 * Define a list of aliases
 */
 enum months {Jan=1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};

 enum months month;        /* define 'month' variable of type 'months' */

 //Verify an arg was passed in
 if(argc != 2) {
    fprintf(stderr, "USAGE: ./month.out <Integer value>\n");
    exit(1);
 }

 int userInput = atoi(argv[1]);

 //Verify the input is greater then zero
 if(userInput <= 0 && userInput <= 12) {
    fprintf(stderr, "USAGE: %d must be 1 <= N <= 12\n", userInput);
    exit(1);
 }

 /* Using aliases print out a statement
 that corresponds to the month the user choose */
 switch(userInput) {
    case 1:
       month = Jan;
       printf("January is the %dst month in the year.\n", month);
       break;
    case 2:
       month = Feb;
       printf("February is the %dnd month in the year.\n", month);
       break;
    case 3:
       month = Mar;
       printf("March is the %drd month in the year.\n", month);
       break;
    case 4:
       month = Apr;
       printf("April is the %dth month in the year.\n", month);
       break;
    case 5:
       month = May;
       printf("May is the %dth month in the year.\n", month);
       break;
    case 6:
       month = Jun;
       printf("June is the %dth month in the year.\n", month);
       break;
    case 7:
       month = Jul;
       printf("July is the %dth month in the year.\n", month);
       break;
    case 8:
       month = Aug;
       printf("August is the %dth month in the year.\n", month);
       break;
    case 9:
       month = Sep;
       printf("September is the %dth month in the year.\n", month);
       break;
    case 10:
       month = Oct;
       printf("October is the %dth month in the year.\n", month);
       break;
    case 11:
       month = Nov;
       printf("November is the %dth month in the year.\n", month);
       break;
    case 12:
       month = Dec;
       printf("December is the %dth month in the year.\n", month);
       break;
 }
}

Output:
lee@isis:~/programming/c/enum$ ./month.out 1
January is the 1st month in the year.
lee@isis:~/programming/c/enum$ ./month.out 2
February is the 2nd month in the year.

Now, you may be asking yourself why you would want to use enum in the first place? Well enum will make your code more readable and cut down on the amount of code that you would have to write to sequentially initialise all those variables.

This concludes the tutorial on enum. I hope you have enjoyed and happy coding!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: