Another way to learn New Technology

Explaining user-defined data types with Example

0 717

Explaining user-defined data types with Example

User-Defined Data Types:

Read This: Fundamental data types and sizes used in C Programming

C also provides users a feature to definite their own data types which can later be used to declare variables. This feature is known as “type definition,” that allows a user to define an identifier that would represent an existing data type. Generally, it takes the form:

typedef type identifier;

Where type refers to an existing data type, and ‘identifier’ refers to the new name given by the user to the data type. Some examples of type definition are:

typedef int marks;

typedefs float price;

They can later be used to definite variables as marks subject 1, subject 2; price item 1, item2;

Another user-defined data type is enumerated data type provided by ANSI standard. It is defined as follows:

enum identifier { value1  value 2, value n };

The identifier here is a user-defined enumerated data type which can be used to declare variables that can have one of the values enclosed within braces.

For Example:

enum day {Sunday, Monday, Tuesday, ..Saturday } enum day week_day 1 ,week_day 2;

Here the enumerated variables week day 1, week_day 2 can take one of the values Sunday, Monday,….Saturday. Each enumerated constant (sometimes called an enumerator) has an integer value. Unless specified, the first constant has a value of zero. The value increase by one for each additional constant in the enumeration. The values of each constant can also be specified.

A sample program:


enum days {Monday = 1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};

int main()


enum days today = Monday;

if ((today == Saturday) || (today == Sunday))


printf (“'Weekend\n”);




print(“Go to work or school\n”);


return 0;


From the sample program given above, Monday equals 1, Tuesday equals 2, and Wednesday equals 3 and so forth.