User Defined Data Types in C

Enumerations, floating points, integers, ASCII characters, pointers to declare variables.

© Guy Lecky-Thompson

Lost Bytes, sxc.hu

In this tutorial, complex and user defined data types and enumerations are explored. Designed for beginner and intermediate programmers in the C and C++ languages.

Introduction

The C programming language provides several basic types:

We can then use these to declare variables - places to store information - which can accommodate data of a specific type. On top of that, we can also define arrays - collections of pieces of data - to allow us to store multiple values under a single variable.

If this is not already familiar territory, the reader should consult:

These are also part of the Computer Programming Topic here at Suite101, and can help to fill in the gaps.

On top of these, C also allows us to create our own types - either as references to built-in types, or multipart types (like a record with fields) - depending on the data we want to model.

Using typedef

The first keyword we shall explore is typedef. With this keyword we can define a new type, and give it a user-friendly name. For example:

typedef int Boolean;

This code tells the compiler that we want to define a type Boolean, that is equivalent to an integer. In order to use this new type, we could use code similar to:

Boolean bResult;
// ... Some code ...
if (bResult == 0) { printf ("False\n"); }

This is not, however, very user friendly, but can be made more so by using the #define keyword to define values for true and false:

#define FALSE 0;
#define TRUE 1;
// ... Some code ...
Boolean bResult;
// ... Some code ...
if (bResult == FALSE) { printf ("False\n"); }

This is fine, but there is en even more elegant way to achieve this.

Enumerated Types (enum)

An enumerated data type is a list of possible values, each of which is assigned a sequential number. This allows us to write code that can compare values easily. So, for our Boolean example:

typedef enum {FALSE, TRUE} Boolean;

This tells the compiler that we would like a Boolean type that evaluates to FALSE, or TURE. The compiler will assign the values 0 and 1 to these new types, enabling comparisons such as:

Boolean bResult;
// ... Some code ...
if (bResult == FALSE) { printf ("False\n"); }

We can also define more complex lists:

typedef enum {Mon, Tue, Wed, Thu, Fri} Workdays;
Workdays Today;
// ... Some code ...
if (Today == Mon) { printf("Monday\n"); }

However, in order to define complex data types which can contain data beyond the basic types, we need two more keywords.

Structures and Unions

The struct keyword allows us to create record style data. For example, if we need to define a new data type to store names, we might create it with:

typedef struct
{
char szSalutation[5]; // Mr, Mrs, Miss, Ms
char szFirstName[15];
char szMiddleInitial[2];
char szLastName[25];
} sName;

So, we can now declare a variable of type sName, and store various values in it:

sName myName;
strcpy(myName.szSalutation,"Mr");
strcpy(myName.szFirstName,"Joe");
strcpy(myName.szMiddleInitial,"B");
strcpy(myName.szLastName,"Loggs");

Of course, we could also combine the above with enumerated types for the static list of values Mr, Mrs, Miss etc. but we'll leave that as a reader exercise for now.

The final keyword that we can use to create user defined types is the untion keyword. This permits us to create a type which can hold multiple values, but only one at a time.

So, if we wanted to create a type to hold either integers or floating point numbers, we would write:

typdef union a_number { int a; float b; }

We can then set either a or b, but not both at the same time, using the regular assignment operator:

a_number example_number;
example_number.b = 42.42;

And... we're done. Questions can be asked through the message board below; so, let's interact!


The copyright of the article User Defined Data Types in C in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish User Defined Data Types in C must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo