User-Defined Data Types In C
Last Updated :
16 Oct, 2023
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, "GeeksforGeeks");
person1.lifespan = 30;
// Accessing and printing structure members
printf("Name: %s\n", person1.company);
printf("Age: %d\n", person1.lifespan);
return 0;
}
OutputName: GeeksforGeeks
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, "GeeksforGeeks, C");
printf("Data.str: %s\n", data.str);
return 0;
}
OutputData.i: 10
Data.f: 3.140000
Data.str: GeeksforGeeks, 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;
}
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 = "GeeksforGeeks";
printf("Best Tutorials on: %s \n", name);
return 0;
}
OutputBest Tutorials on: GeeksforGeeks
Similar Reads
C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to
5 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Inheritance in C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio
10 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read