What Are the User Defined Data Type in C Explain With Example?


Larry Thompson

A user-defined data type in C is a data type that is created by the programmer. It allows the programmer to define a new data type based on existing data types or structures provided by the language. This can be useful for organizing and encapsulating related data into a single unit.

To understand user-defined data types, let’s consider an example of creating a user-defined structure to represent a student’s information. We can define a structure called “student” that contains attributes such as name, age, and grade.


struct student {
  char name[50];
  int age;
  int grade;

In the above code, we have defined a structure named “student” using the struct keyword. It has three attributes: name (a character array of size 50), age (an integer), and grade (an integer). These attributes collectively represent the information of a student.

Now, we can create variables of this user-defined data type just like any other built-in data types.


struct student s1;

Here, we have created a variable named “s1” of type “student”. We can then access and modify its attributes using the dot operator.


strcpy(s1.name, "John Doe");
s1.age = 18;
s1.grade = 12;

In the above code snippet, we have assigned values to the attributes of “s1”. The name attribute is assigned using the strcpy() function from string.h header file.

User-defined data types provide flexibility and allow us to create complex structures that suit our specific needs. We can use them to represent various entities in our programs, such as employees, products, or any other custom entity.

Advantages of User-Defined Data Types:

  • Modularity: User-defined data types help in organizing related data into a single unit, making the code more modular and easier to maintain.
  • Abstraction: They allow us to hide the implementation details of a data type and provide a clean interface for using it.
  • Reusability: Once defined, user-defined data types can be used in multiple parts of the program, promoting code reuse and reducing redundancy.

In C programming, user-defined data types offer a way to create custom structures that encapsulate related data. They provide flexibility and modularity to the program.

By defining our own data types, we can represent complex entities in a more organized manner. Users should make use of these user-defined data types according to their specific requirements.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy