User-Defined Data Types In C
Data Types are the types of data that can be stored in memory using a programming language. Basically, data types are used to indicate the type of data that a variable can store. These data types require different amounts of memory and there are particular operations that can be performed on them. These data types can be broadly classified into three types:
- Primitive Data Types
- Derived Types
- User Defined Data Types
In this article, will discuss the User-Defined Data Type.
What is a user-defined Datatype in C?
The data types defined by the user themself are referred to as user-defined data types. These data types are derived from the existing datatypes.
Need of User-Defined Datatypes
- It enables customization in the code.
- Users can write more efficient and flexible code.
- Provides abstraction.
Types of User-Defined DataTypes
There are 4 types of user-defined data types in C. They are
- Structure
- Union
- Enum
- Typedef
1. Structure
As we know, C doesn’t have built-in object-oriented features like C++ but structures can be used to achieve encapsulation to some level. Structures are used to group items of different types into a single type. The “struct” keyword is used to define a structure. The size of the structure is equal to or greater than the total size of all of its members.
Syntax
struct structure_name { data_type member_name1; data_type member_name1; .... .... };
Example
C
// C Code to implement a struct #include <stdio.h> // Defining a structure struct Person { char company[50]; int lifespan; }; int main() { // Declaring a variable of the structure type struct Person person1; // Initializing structure members strcpy (person1.company, "w3wiki" ); person1.lifespan = 30; // Accessing and printing structure members printf ( "Name: %s\n" , person1.company); printf ( "Age: %d\n" , person1.lifespan); return 0; } |
Name: w3wiki Age: 30
2. Union
Unions are similar to structures in many ways. What makes a union different is that all the members in the union are stored in the same memory location resulting in only one member containing data at the same time. The size of the union is the size of its largest member. Union is declared using the “union” keyword.
Syntax
union union_name { datatype member1; datatype member2; ... };
Example
C
// C Code to implement Union #include <stdio.h> // Declaring a union union Data { int i; float f; char str[20]; }; int main() { // creating an instance named 'data' of union Data union Data data; data.i = 10; printf ( "Data.i: %d\n" , data.i); data.f = 3.14; printf ( "Data.f: %f\n" , data.f); strcpy (data.str, "w3wiki, C" ); printf ( "Data.str: %s\n" , data.str); return 0; } |
Data.i: 10 Data.f: 3.140000 Data.str: w3wiki, C
2. Enumeration (enums)
Enum is short for “Enumeration”. It allows the user to create custom data types with a set of named integer constants. The “enum” keyword is used to declare an enumeration. Enum simplifies and makes the program more readable.
Syntax
enum enum_name {const1, const2, ..., constN};
Here, the const1 will be assigned 0, const2 = 1, and so on in the sequence.
We can also assign a custom integer value such as:
enum enum_name { const1 = 8; const2 = 4; }
Example
C
// C code to implement enum #include <stdio.h> // Declaring a enum enum Cafes { Dyu_Art_Cafe, Tea_Villa_Cafe, The_Hole_in_the_Wall_Cafe, Cafe_Azzure, The_Banaglore_Cafe, Dialogues_Cafe, Cafe_Coffee_Day }; // driver code int main() { enum Cafes today = The_Banaglore_Cafe; printf ( "Today is %d\n" , today); return 0; } |
Today is 4
Typedef
typedef is used to redefine the existing data type names. Basically, it is used to provide new names to the existing data types. The “typedef” keyword is used for this purpose;
Syntax
typedef existing_name alias_name;
Example
C
// C Code to implement Typedef #include <stdio.h> typedef char Company; int main() { Company* name = "w3wiki" ; printf ( "Best Tutorials on: %s \n" , name); return 0; } |
Best Tutorials on: w3wiki
Contact Us