Monday, January 4, 2010

9.3 USER-DEFINED DATA TYPES (typedef)

The typedef feature allows users to define new data-types that are equivalent to existing data types.
In general terms, a new data type is defined as

typedef type new-type;

where type refers to an existing data type (either a standard data type, or previous user-defined data  type ), and new-type refers to the new user defined data type.It should be understood that the new data type will be new only in terms of name but in reality it is fundamentally same as the standard data types.

Here is a simple declaration involving the use of typedef.

typedef  int  age;

In this declaration age is a user defined data type, which is equivalent to type int.
Hence, the variable declaration

age male, female;

is equivalent to

int male, female;

The typedef is particularly convenient when defining structures, since it eliminates the need to write the struct tag  whenever a structure is referenced.

Let us now look at the following declarations

typedef  struct   {
int acct_no;
char acct_type;
char name[80];
float balance;

} record;

record oldcustomer, newcustomer;

The first declaration defines record as a user defined data type.The second declaration defines old customer and new customer as structure variables of type record.

No comments:

Post a Comment