Enumerator
To help make programs more readable, the C language allows users to define their own types. Two forms are provided:
This is really just an integer declaration with symbolic names for the values variables of this type can take. Essentially, an enum takes the place of an integer declaration and a set of macros.
For example, instead of using the macros defined in tfdef.h, we can declare an "enumerated type" as:
enum boolean {FALSE,TRUE};This defines a type enum boolean (really an integer) that can take on the values FALSE (really 0) or TRUE (really 1). We can then declare a variable of this type:
enum boolean flag;and can then write statements like:
flag = TRUE; if(flag == FALSE)The general form to declare an enumerated type is:
enum [<tag>] {<identifier> [,<identifier>...} [<variable_name>];The <tag> is an optional name for the type, and the <identifier>'s are the symbolic names for the values we can assign to a variable of this type. They correspond to integer values starting with 0, from left to right. The optional <variable_name> allows us to declare a variable of this type at the same time we declare the type.
When we declare an enum type, we can also specify the starting integer value for the symbolic names:
enum days {SUN=1,MON,TUE,WED,THU,FRI,SAT}; enum days today, tomorrow; today = MON; tomorrow = today + 1; if(tomorrow == SAT) play();We can also give our own names to any existing type using typedef. The general form is:
typedef <existing type name> <new type name>;For example:
typedef float coefficient; typedef int solution; solution s1, s2, s3; //these variables are ints coefficient c1, c2, c3;// These variables are floats