User Defined Data Types in C Language
In C programming language we have many user defined data types like structure, union, bit field, enumerator, typedef etc. The most commonly used are union and structure. These data types help programmers manage complex data efficiently and improve program organization. Understanding user defined data types is an important part of C programming and software development. Students studying in top bca colleges are introduced to these concepts to build a strong foundation in programming and application development.
Structure:
A structure is a convenient tool for handling a group of logically related data items. Structure help to organize complex data is a more meaningful way. It is powerful concept that we may after need to use in our program Design.
A structure is a collection of variable types grouped together. You can refer to a structure as a single variable and to its parts as members of that variable by using the dot (.) operator. The power of structures lies in the fact that once defined; the structure name becomes a user-defined data type and may be used the same way as other built-in data types, such as int, double and chars.
Defining a Structure
To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member for your program. The format of the struct statement is this:
struct [structure tag]
{
member definition;
member definition;
...
member definition;
} [one or more structure variables];
Example
1. Struct account
{
Int accountno
Char name[50];
Float balance;
}customer[20]
2. struct date
{
Int month;
Int day;
Int year;
}dateofbirth;
In these examples customer is a structure array of type account and date of birth is a structural type of date.
With in a structure members can be structures. In the following example of biodata structure date which is a structure is a member
Example
struct date
{
Int day;
Int month;
Int year;
}
Struct biodata
{
Name char[30];
Int age ;
Date birthdate;
}staff[30];
Here staff is an array of structure of type biodata
Unions
Union is user defined data type used to stored data under unique variable name at single memory location.
A union is a special data type available in C that enables you to store different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multi-purpose.
Defining a Union
If we are having the less memory to use in our program, for example 64K, we can use a single memory location for more than one variable this is called union.
You can use the unions in the following locations.
- You can share a single memory location for a variable myVar1 and use the same location for myVar2 of different data type when myVar1 is not required any more.
- To define a union, you must use the union statement in very similar was as you did while defining structure. The union statement defines a new data type, with more than one member for your program. The format of the union statement is as follows:
union [union tag]
{
member definition;
member definition;
...
member definition;
} [one or more union variables];
The memory occupied by a union will be large enough to hold the largest member of the union.
Following is the example which will display total memory size occupied by the above union:
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
printf( "Memory size occupied by data : %d\n", size of(data));
return 0;
}
Output
Memory size occupied by data : 20
Author
Mr.Rahul Agrwal
Associate Professor, Department Of CS & I.T.
Biyani Group Of colleges, Jaipur