User Defined Data Types In C

1. Structures (struct)

A structure is a grouping of variables (of varying data types) combined within a single identifier. This aids in the logical organization of interconnected data.

Syntax:

It has the following syntax:

Example

struct structure_name {
    data_type member1;
    data_type member2;
    ...
};

Parameters:

  • structure name: The name of the structure.
  • member1, member2: Variables of different types within the structure.
  • Example:

Let's consider an example to demonstrate the struct in C programming.

Example

#include <stdio.h>
 
// Define a structure
struct Student {
    char name[50];
    int age;
    float marks;
};
 
int main() {
    struct Student s1 = {"John", 20, 85.5};
    
    // Displaying structure data
    printf("Student Name: %s\n", s1.name);
    printf("Age: %d\n", s1.age);
    printf("Marks: %.2f\n", s1.marks);
    
    return 0;
}

Output:

Output

Student Name: John  
Age: 20  
Marks: 85.50

Advantages of struct:

Several advantages of struct in C are as follows:

  • It groups related data together.
  • It improves code clarity.
  • It enables complex data management, such as records in databases.
  • Use Cases of struct:

Several use cases of struct in C are as follows:

  • Defining objects like students, employees, books, etc.
  • Storing multiple data fields together (e.g., 3D coordinates).
  • Using linked lists and other data structures .
  • Unions (union)

A union is akin to a structure, however, all its members occupy the same memory location, leading to memory conservation when only one member is accessed.

Syntax:

It has the following syntax:

Example

union union_name {
    data_type member1;
    data_type member2;
    ...
};

Example:

Let's consider a scenario to demonstrate the concept of union in the C programming language.

Example

#include <stdio.h>
union Data {
    int i;
    float f;
    char str[20];
};
 
int main() {
    union Data data;
    
    data.i = 10;
    printf("Data.i: %d\n", data.i);
    
    data.f = 220.5;
    printf("Data.f: %.2f\n", data.f);
    
    return 0;
}

Output:

Output

Data.i: 10  
Data.f: 220.50

Note: The value of i is overwritten when f is assigned a value.

Benefits of union:

Several advantages of the union in C include:

  • Efficient management of memory resources.
  • Enabling the handling of diverse data types dynamically within applications.
  • Applications of union:

Several advantages of the union in C include:

-

  • Storing different data types in a single memory location.

-

  • Employed in embedded systems to optimize space usage.
  • Enumerations (enum)

An enumeration is a custom data type utilized to associate labels with a collection of numerical values, enhancing the clarity of the code.

Syntax:

It has the following syntax:

Example

enum enum_name { constant1, constant2, ... };

Example:

Let's consider an example to demonstrate the enum in the C programming language.

Example

#include <stdio.h>
 
enum Color { RED, GREEN, BLUE };
 
int main() {
    enum Color myColor = GREEN;
    printf("My favorite color is: %d\n", myColor);
    
    return 0;
}

Output:

Output

My favorite color is: 1

(By default, RED = 0, GREEN = 1, BLUE = 2.)

Benefits of enum:

Several advantages of utilizing enums in C include:

  • Enhancing the readability of code.
  • Minimizing errors caused by arbitrary integers.
  • Applications of enum:

Several ways in which the union in C is utilized include:

  • Employed for depicting states such as success or failure.
  • Utilized for declaring constants with meaningful identifiers.
  • Type Definition (Typedef)

In C++, the typedef keyword serves as a tool that assigns an alternative name (alias) to pre-existing data types. This functionality plays a crucial role in enhancing the clarity and manageability of code, as well as facilitating the reusability of complex data types involving pointers, structures, and even function pointers in software development scenarios.

Key Features of Typedef:

Several key features of Typedef in C are as follows:

  • Alias to Data Type: It can be defined as providing alternative names to existing types.
  • Enhances Code Readability: It creates simplicity among complex data types making it easier to read.
  • Increases Code Maintainability: If any modification is made to a data type, it is just sufficient to change its corresponding typedef statement.
  • Used for Structures and Pointers: It is mostly used for structs, enums, and pointers to enhance clarity.
  • Comparison of User-Defined Data Types in C:

Here is the breakdown of various custom data types in the C programming language.

Feature Structure (struct) Union (union) Enumeration (enum) Type Definition (typedef)
Definition It groups different data types together. It stores different data types in the same memory location. It assigns names to integer constants. It creates an alias for existing data types.
Memory Usage Memory allocated for all members separately. Memory is shared among members based on the largest member. It requires only an integer value. It does not consume extra memory.
Data Storage It can store multiple values at once. It can store only one value at a time. It stores predefined integer constants. It stores aliases for types.
Modifiability Each member can be modified independently. Modifying one member overwrites others. Values remain constant. It improves maintainability by renaming types.
Code Readability It improves readability by organizing related data. It can be confusing due to shared memory. It increases clarity by replacing numbers with names. It makes complex type declarations more understandable.
Use Case Representing real-world objects like employees, students, and books. Memory-efficient type for storing multiple representations of the same data. Defining options, states, or categories. Simplifying code by creating new type names.
Data Type It can hold multiple data types. It holds only one active data type at a time. It only holds integer values. It can represent any data type.

Memory Management Comparison:

Here is the comparison of memory management for various custom data types in the C programming language.

Data Type Memory Allocation Size Calculation Example Usage
Structure (struct) It allocates memory for each member separately. Sum of the sizes of all members (plus padding). It is used when multiple related values must be stored together.
Union (union) All members share the same memory. Size equals the largest member. It is used when multiple variables occupy the same memory space to save memory.
Enumeration (enum) It allocates memory only for an integer. It has fixed integer size (usually 4 bytes). It is used when defining sets of constants.
Type Definition (typedef) It has no extra memory required. It depends on the original data type. It is used to rename complex data types.

Advantages and Disadvantages of Each User-Defined Data Type:

Data Type Advantages Disadvantages
Structure (struct) It organizes related data logically, and allows multiple members with different types. It consumes more memory than unions.
Union (union) It saves memory by sharing space among members. Only one member can hold a value at a time.
Enumeration (enum) It enhances readability and code maintainability. Limited to integer values.
Type Definition (typedef) It improves code clarity and portability. It can sometimes reduce code flexibility by hiding details.

Conclusion:

In summary, custom data types in C provide a structured and effective way to handle intricate data. Structs help group related variables, unions minimize memory usage, enums enhance code readability, and typedef simplifies maintenance tasks. Proficiency in utilizing these features enhances program efficiency, clarity, and scalability.

Input Required

This code uses input(). Please provide values below: