Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

The GNU C Programming Tutorial - enum

Node:enum, Next:, Previous:More data types, Up:More data types



enum

The enum type specifier is short for "enumerated data". The user can define a fixed set of words that a variable of type enum can take as its value. The words are assigned integer values by the compiler so that code can compare enum variables. Consider the following code example:

#include <stdio.h>

/* To shorten example, not using argp */
int main ()
{
  enum compass_direction
  {
    north,
    east,
    south,
    west
  };

  enum compass_direction my_direction;
  my_direction = west;

  return 0;
}

This example defines an enumerated variable type called compass_direction, which can be assigned one of four enumerated values: north, east, south, or west. It then declares a variable called my_direction of the enumerated compass_direction type, and assigns my_direction the value west.

Why go to all this trouble? Because enumerated data types allow the programmer to forget about any numbers that the computer might need in order to process a list of words, and simply concentrate on using the words themselves. It's a higher-level way of doing things; in fact, at a lower level, the computer assigns each possible value in an enumerated data type an integer cconstant -- one that you do not need to worry about.

Enumerated variables have a natural partner in the switch statement, as in the following code example.

#include <stdio.h>

enum compass_direction
{
  north,
  east,
  south,
  west
};


enum compass_direction get_direction()
{
  return south;
}

/* To shorten example, not using argp */
int main ()
{
  enum compass_direction my_direction;
  puts ("Which way are you going?");
  my_direction = get_direction();

  switch (my_direction)
  {
    case north:
      puts("North? Say hello to the polar bears!");
      break;

    case south:
      puts("South? Say hello to Tux the penguin!");
      break;

    case east:
      puts("If you go far enough east, you'll be west!");
      break;

    case west:
      puts("If you go far enough west, you'll be east!");
      break;
  }

  return 0;
}

In this example, the compass_direction type has been made global, so that the get_direction function can return that type. The main function prompts the user, Which way are you going?, then calls the "dummy" function get_direction. In a "real" program, such a function would accept input from the user and return an enumerated value to main, but in this case it merely returns the value south. The output from this code example is therefore as follows:

Which way are you going?
South? Say hello to Tux the penguin!

As mentioned above, enumerated values are converted into integer values internally by the compiler. It is practically never necessary to know what integer values the compiler assigns to the enumerated words in the list, but it may be useful to know the order of the enumerated items with respect to one another. The following code example demonstrates this.

#include <stdio.h>

/* To shorten example, not using argp */
int main ()
{
  enum planets
  {
    Mercury,
    Venus,
    Earth,
    Mars,
    Jupiter,
    Saturn,
    Uranus,
    Neptune,
    Pluto
  };

  enum planets planet1, planet2;

  planet1 = Mars;
  planet2 = Earth;

  if (planet1 > planet2)
    puts ("Mars is farther from the Sun than Earth is.");
  else
    puts ("Earth is farther from the Sun than Mars is.");

  return 0;
}

The output from this example reads as follows:

Mars is farther from the Sun than Earth is.

 
 
  Published under free license. Design by Interspire