StudyLover
  • Home
  • Study Zone
  • Profiles
  • Typing Tutor
  • B Tree
  • Contact us
  • Sign in
StudyLover C Programming: User-Defined Data Types 🧑‍🎨
Download
  1. C Programming
  2. Unit 1: Foundations of Problem Solving & C Language Basics
C Programming: Derived Data Types : C Programming: Type Conversions
Unit 1: Foundations of Problem Solving & C Language Basics

User-defined data types allow programmers to create their own data types by grouping existing data types. This is a powerful feature for creating more organized, readable, and meaningful code, especially when dealing with complex data that represents real-world objects.

The main user-defined types in C are struct, union, and enum. The typedef keyword is also used extensively with these types.


1. Structure (struct) 🏗️

A structure is a complex data type that groups together one or more variables of potentially different data types under a single name. Think of it as a template for a record, like a student's ID card which has a name (string), a roll number (integer), and a GPA (float).

·         Purpose: To represent real-world entities and keep related data together.

·         Syntax to Define:

C

struct StructureName {

    dataType member1;

    dataType member2;

    ...

};

Example

This example defines a struct to store information about a car.

C

#include <stdio.h>

#include <string.h>

 
// Define a structure named 'Car'

struct Car {

    char make[20];

    char model[20];

    int year;

};

 
int main() {

    // Declare a variable 'myCar' of type 'struct Car'

    struct Car myCar;

 
    // Access members using the dot (.) operator and assign values

    strcpy(myCar.make, "Toyota");

    strcpy(myCar.model, "Camry");

    myCar.year = 2023;

 
    // Print the members of the structure

    printf("Car Make: %s\n", myCar.make);

    printf("Car Model: %s\n", myCar.model);

    printf("Car Year: %d\n", myCar.year);

 
    return 0;

}


 

2. Union (union) 🔄

A union is a special user-defined data type that allows storing different data types in the same memory location. While a structure allocates enough space for all its members, a union allocates space equal to its largest member. You can only use one member of the union at a time.

·         Purpose: Primarily used for memory optimization in situations where you only need to store one value out of a set of possible types at any given moment.

·         Key Difference from struct: Members of a union share memory.

Example

This example shows how changing one member of the union affects the others because they all share the same memory space.

C

#include <stdio.h>

 
union Data {

    int i;

    float f;

};

 
int main() {

    union Data data;

 
    data.i = 10;

    printf("Value of i: %d\n", data.i); // i is active, so this is correct

 
    data.f = 220.5;

    printf("Value of f: %f\n", data.f); // f is now active

 
    // The value of 'i' is now corrupted because 'f' is using the memory

    printf("Value of i after changing f: %d\n", data.i); 

 
    return 0;

}


 

3. Enumeration (enum) 📋

An enumeration is a special data type that consists of a set of named integer constants. It makes the code more readable and less prone to errors by using descriptive names instead of "magic numbers."

·         Purpose: To assign meaningful names to a set of integral constants. By default, the first name is 0, the second is 1, and so on.

Example

This example uses an enum to represent the days of the week, making the switch statement very clear.

C

#include <stdio.h>

 
// Define an enumeration for days of the week

enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };

 
int main() {

    enum Day today = Wednesday;

 
    switch (today) {

        case Sunday:

            printf("It's Sunday.\n");

            break;

        case Monday:

            printf("It's Monday.\n");

            break;

        // ... other days

        default:

            printf("It's some other day.\n");

    }

    

    // Enums are integers internally

    printf("Wednesday is day number: %d\n", today); // Will print 3

 
    return 0;

}


 

typedef Keyword

The typedef keyword is used to create an alias or a new, simpler name for an existing data type. It doesn't create a new type but just provides an alternative name.

·         Purpose: To simplify complex type names and improve code readability, especially with structures.

Example

This example uses typedef to create a simpler alias Car for struct Car, so we don't have to write struct every time.

C

#include <stdio.h>

#include <string.h>

 
// Use typedef to create an alias 'Car' for 'struct Car'

typedef struct Car {

    char model[20];

    int year;

} Car; // The new name 'Car' is now an alias

 
int main() {

    // We can now use 'Car' directly instead of 'struct Car'

    Car myCar; 

 
    strcpy(myCar.model, "Civic");

    myCar.year = 2024;

 
    printf("Model: %s, Year: %d\n", myCar.model, myCar.year);

 
    return 0;

}

 

C Programming: Derived Data Types C Programming: Type Conversions
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website