Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

User- Defined Data Types in C

The UDT (User-Defined Data Type) is a typical data type that we can derive out of any existing data type in a program. We can utilise them for extending those built-in types that are already available in a program, and then you can create various customized data types of your own.

In this article, we will take a closer look at the User-Defined Data Types in C according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to know more.

Table of Contents

Why do we Need User-Defined Data Types in C?

The data types originally present in a program may not offer a wide variety of functions. But despite the various basic as well as derived data types present in the C language, there is a special feature using which we can define custom data types of our own, on the basis of our needs. The User-Defined Data Types are basically defined by a user according to their will. These offer various functions on the basis of how one defines them. Thus, these are termed to be “User-Defined”.

Example

Let us take a look at an example where we define a structure in a C program.

struct student

{

char name[100];

int roll;

float marks;

}

In this case, we have created a structure that will further allow us to create a data type of our own. Here’s how we can do it:

struct student info;

In this case, the UDT is a “student”. Here, the info is a variable that will hold the roll number, marks, and name of the student.

Types of User-Defined Data Types in C

The C language allows a feature known as the type definition. This basically allows a programmer to provide a definition to an identifier that will represent a data type which already exists in a program. The C program consists of the following types of UDT:

  • Structures
  • Union
  • Typedef
  • enum

Structures

We use the structure for organising a group of various data items into one single entity – for grouping those data items that are related but might belong to different data types.

The structure data types are related (usually), such as the different sets of information regarding a person, an account, or a part, etc. Every data item present in a structure is known as a member. These members are sometimes also known as fields.

We use the struct keyword for creating a structure. The primary advantage of using a structure is that it becomes very easy for a person to access the members. It is because the allocation of all the members that belong to a specific structure is in the continuous memory. Thus, structure helps in minimising the memory access time for any program.

Generally, we declare a structure as follows:

struct name_of_tag {

data_typea svar_a;

data_typeb svar_b;

data_typec svar_c;

….

….

data_typez svar_z;

};

The beginning of the declaration of a structure typically begins with the struct keyword. We must enclose the declaration of all the members of the structure in braces. Here, the tag_name refers to the identifier that would specify the name of the new structure.

The structure declaration reserves no storage space – but the structure definition would lead to the creation of the structure variables.

We can define the structure variables as follows:

struct tag_name svar_a, svar_b …svar_z;

An example would help us understand this better:

struct sample {

int p, q;

float r, s;

char t, u;

};

struct sample vA, vB, vC; //definition of structure

Union

A union is basically a collection of various different data types that are present in the C language, but not very similar to each other. The union mainly allows the storage of those data types that are different from each other in the very same memory location.

Any user will be able to define a union using various members, but at any given time, just a single member would be able to contain the given value. The unions provide us with an efficient way in which we can use the very same memory location for multiple purposes.

Note that the structures and unions are very similar to each other. The only difference between them is that we can access just a single member of the Union at any given time. It is because the creation of memory is only for one member that has the biggest size (or the highest number of bytes).

We declare the union using the union keyword, and we can access all the members of a Union using the (.) dot operator.

The definition and declaration of a union would go like this:

union tag_name {

data_typea uvar_a;

data_typeb uvar_b;

……

data_typez uvar_z;

};

union tag_name uvar_a, uvar_b,….uvar_z;

We will understand this better with the help of an example,

union sample {

int duration;

float cost;

char keyword;

};

union sample x;

In the example mentioned above, the allocation of about 4 bytes of memory occurs to the union variable x. Thus, we can access the members as x.duration, x.cost, x.keyword, etc., but we can only access one of the members at any given time since the very same memory exists for all three members here.

Typedef

We use the keyword typedef for creating an alias (a new name) for a data type that already exists. The typedef won’t create any new form of data type.

When using the typedef data type, the syntax would be:

typedef         existing_data_type         new_type;

Let us consider the example as follows:

#include <stdio.h>

void main() {

typedef int Lessons; //statement-1

Lessons x = 17;

printf(“Given value =%d\n”, x);

}

Let us look at the statement here. We have used the typedef statement for creating Lessons as an alias for int. After we use this statement, Lessons will become the new name for the int data type in the program given above. Also, the variables that are declared in the form of Lessons data type will behave like the int variables in all the practical implications of the program.

enum

The enum refers to a keyword that we use for creating an enumerated data type. The enum is basically a special data type (enumerated data type) that consists of a set of various named values – known as members or elements. We mainly use it for assigning different names to the integral constants. This way, the program becomes much more readable.

Here is the format that we use for creating the enum type:

enum identifier (value_a, value_b, …. , value_z);

The enumerated data types basically allow a user to create symbolic names of their own for a list of all the constants that are related to each other.

For instance, you can create the enumerated data type for the true and false conditions this way:

enum Boolean {

true,

false

};

In case we don’t assign values explicitly to the enum names; the compiler, by default, would assign the values that start from 0.

In this case, true and false would be automatically assigned to 1 and 0 respectively. In simpler words, the first field of the enum value here will be replaced by 1, and then the next field will be replaced with 0, and so on.

Let us take a look at an example to display how to use the enum data value.

#include <stdio.h>

void main() {

enum week {MON = 1, TUE, WED, THURS, FRI, SAT, SUN};

enum week off = FRI;

printf(“Week Off = %d”, birthday);

}

Here, the field name MON will be assigned with the value 1. Thus, the next field name TUE will be assigned with the value 2 automatically, and so on.

The program mentioned above will print the following output:

Week Off = 5

Practice Problems on User-Defined Data Types in C

1. Which of these is a User-Defined Data Type in the C programming language?

1. Structures

2. Qualifier

3. Typedef

4. Union

5. enum

A. 1, 2, 3, and 4

B. 1, 3, 4, and 5

C. 2, 4, and 5

D. 1, 2, 3, and 5

Answer – B. 1, 3, 4, and 5

2. The output of the program mentioned below would be:

#include <stdio.h>

void main() {

enum months {January = 1, February, March, April, May, June, July, August, September, October, November, December};

enum surprise birthday = August;

printf(“Surprise Birthday = %d”, months);

}

A. Surprise Birthday = 5

B. Surprise Birthday = 12

C. Surprise Birthday = 8

D. Error

Answer – C. Surprise Birthday = 8

Here, the field name January will be assigned with the value 1. The next field name February will be assigned with the value 2 automatically, and so on.

Thus, the program will print the following output:

Surprise Birthday = 5


Frequently Asked Questions

Q1

What is the difference between a structure and a union?

Note that structures and unions are very similar to each other. The only difference between them is that we can access just one single member of the Union at any given time. It is because the creation of memory is only for the one member that has the biggest size (or the highest number of bytes).
We use the struct keyword for creating a structure. We declare the union using the union keyword, and we can access all the members of a Union using the (.) dot operator.

Q2

Why do we need User-Defined Data Types in C?

The data types originally present in a program may not offer a wide variety of functions. But despite the various basic as well as derived data types present in the C language, we get a special feature using which we can define custom data types of our own, on the basis of our needs. These user-defined data types are basically defined by a user according to their will. They offer various functions on the basis of how one defines them. Thus, these are termed to be “user-defined”.

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility CriteriaGATE 2023GATE Admit CardGATE Syllabus for CSE (Computer Science Engineering)GATE CSE NotesGATE CSE Question Paper, and more.

Also Explore,

Comments

Leave a Comment

Your Mobile number and Email id will not be published.

*

*