|
|
|
|
|
|
|
ular program. Much of the remainder of this book is about user-defined data types. In this section, we examine how to create our own simple types. |
|
|
|
|
|
|
|
|
In Chapter 5, we introduced the Typedef statement, whose syntax is given by |
|
|
|
|
|
|
|
|
typedef ExistingTypeName NewTypeName ; |
|
|
|
|
|
|
|
|
|
To simulate a Boolean data type, we used Typedef to introduce Boolean as a synonym for int: |
|
|
|
|
|
|
|
|
typedef int Boolean;
const Boolean TRUE = 1;
const Boolean FALSE = 0;
.
.
.
Boolean dataOK;
.
.
.
dataOK = TRUE; |
|
|
|
|
|
|
|
|
The Typedef statement provides a very limited way in which to define our own data types. In fact, Typedef does not create a new data type at all; it merely creates an additional name for an existing data type. As far as the compiler is concerned, the domain and operations of our Boolean type are identical to the domain and operations of the int type. |
|
|
|
|
|
|
|
|
Despite the fact that Typedef cannot truly create a new data type, it is a valuable tool for writing self-documenting programs. Program code that uses the identifiers Boolean, TRUE, and FALSE is more descriptive than code that uses int, 1, and 0. |
|
|
|
|
|
|
|
|
Names of user-defined types obey the same scope rules that apply to identifiers in general. Most types like Boolean are defined globally, although it is reasonable to define a new type within a subprogram if that is the only place it is used. The guidelines that determine where a named constant should be defined also apply to data types. |
|
|
|
|
|