[previous_group] * [up] [next] [next_group]

User Defined Types

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)

We can use the newly declared type anywhere we must provide a type specifier. For example
     enum boolean verify( float a11, float a12, float c1, ...

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:
     typdef enum boolean boolean;
     typedef float coefficient;
     typedef double solution;

     boolean verify( coefficient a11, coefficient a12, coefficient c1,
                     coefficient a21, coefficient a22, coefficient c2,
                     solution x1, solution x2, double error);

[up] to Overview.