User Defined Data Types in C

C language allows a programmer to create user defined data types. It provides 2 keywords to do so; typedef and enum.

typedef
typedef allows a programmer to define an identifier that would represent an existing data type. Therefore, the identifier so defined can be later used to declare variables. The general syntax to do so is as follows:

typedef data-type identifier;

The existing data type can belong to any class of data types; primary, derived and even user-defined. It should be noted that the user-defined data type so created does not actually create a new data type, rather it creates a new name for the data type. As an example,

                typedef int rollno;

This will create another name, rollno, for int data type. Therefore, the new name can also be used to declare variables and it is as good as the existing data type. It is worth mentioning here that int keyword still exists and thus can be used to declare variables.

Hence, the statement
  rollno student1, student2;
is as good as statement
int student1, student2;

One may argue what for can this be used? The answer is using this mechanism the readability of a program is enhanced.

enum
enum is an enumerated data type which can be used to declare variables that can have one of the values enclosed within the braces. The general syntax to do so is as follows:

enum        identifier         {value1,         value2,          ….           valuen};

Now, the identifier can be used to declare variables and these so declared variables can only hold one of the values within the curly braces. As an example,

enum pgdca { sem1, sem2, sem3, sem4};

declares a user-defined enumerated data type pgdca which can only hold values as sem1, sem2, sem3 and sem4.

pgdca student1=sem1, student2=sem2;

The above statement declares 2 variables of type pgdca and initializes them to some legitimate values.