User – defined data type (typedef) In C

C Supports a feature known as typedef that allows users to define an identifier that would represents an existing data type.The new data type names are defined using the keyword typedef. The user-defined data type identifier can later be used to declare variable. The general format of typedef is

Typedef existing-type new-type;

For example:

typedef int unit;
typedef float mark:

unit is a user-defined data type which is equivalent to type int and mark is equivalent to type float.

Hence the variable declaration

Unit x,y,z is equivalent to int x,y,z;

Similarly,mark a,b,c;is equivalent to float a,b,c;

Further the declaration

Typedef float height[10]; /*define height as a 10 elements array of float type. */
Height men,women; /*men and women are 10 elements array of float type */

Is equvalent to

type def float height;
height men[10], women[10];

The typedef featuer is particularly convenient when defining structure, since it eliminates repeatedly to write struct tag whenever structure is referenced.it can be used with the structure to define an alisa name for structure. The general format for userdefined structure is

ypedef  struct
{
Member1;
Member2;
.   .    .   .   .
.   .    .   .   .
.    .    .   .   .
Membern;
}new-type;

Where new-type is the user-defined structure type.

For example:

Typedef struct
{
    Int rollno;
    Char name[20];
    Int age;
    Float fees;
}student;
Student  student1, student2,  student3;

It creates three structure variables such as student1, student2 and student3.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext