⬅ Previous Next ➡

User-Defined Data Types

User-Defined Data Types in C
  • User-defined data types allow you to create your own data models in C.
  • Main user-defined types: struct, union, enum, and the typedef keyword.
  • Used to store related data together and make code more readable and structured.

1) Structures (struct)

  • Structure groups different data types under one name.
  • Each member has its own separate memory.
#include <stdio.h>
#include <string.h>

struct Student {
    int id;
    char name[30];
    float marks;
};

int main() {
    struct Student s1;

    s1.id = 1;
    strcpy(s1.name, "Sourav");
    s1.marks = 88.5f;

    printf("ID: %d\n", s1.id);
    printf("Name: %s\n", s1.name);
    printf("Marks: %.2f\n", s1.marks);

    return 0;
}

2) Array of Structures

  • Useful to store multiple records (like student list).
#include <stdio.h>

struct Student {
    int id;
    char name[30];
};

int main() {
    struct Student st[2] = {
        {1, "Amit"},
        {2, "Sourav"}
    };

    for (int i = 0; i < 2; i++) {
        printf("%d %s\n", st[i].id, st[i].name);
    }

    return 0;
}

3) Nested Structures

  • A structure inside another structure.
  • Used for complex data (address inside student, etc.).
#include <stdio.h>

struct Address {
    char city[20];
    int pin;
};

struct Student {
    int id;
    char name[30];
    struct Address addr; // nested structure
};

int main() {
    struct Student s = {1, "Sourav", {"Bhubaneswar", 751001}};

    printf("ID: %d\n", s.id);
    printf("Name: %s\n", s.name);
    printf("City: %s\n", s.addr.city);
    printf("PIN: %d\n", s.addr.pin);

    return 0;
}

4) Structures with Pointers (-> operator)

  • Use pointer to structure to access members using ->.
#include <stdio.h>

struct Student {
    int id;
    char name[20];
};

int main() {
    struct Student s = {1, "Sourav"};
    struct Student *p = &s;

    printf("ID: %d\n", p->id);
    printf("Name: %s\n", p->name);

    return 0;
}

5) Unions (union)

  • Union is like structure, but all members share the same memory.
  • Total size = size of largest member.
  • Only one member value is valid at a time.
#include <stdio.h>

union Data {
    int i;
    float f;
    char ch;
};

int main() {
    union Data d;

    d.i = 10;
    printf("i = %d\n", d.i);

    d.f = 2.5f; // overwrites same memory
    printf("f = %.2f\n", d.f);

    d.ch = 'A'; // overwrites same memory
    printf("ch = %c\n", d.ch);

    return 0;
}

6) Enumeration (enum)

  • enum defines named integer constants.
  • By default: first = 0, next = 1, ... (can assign custom values).
#include <stdio.h>

enum Day {SUN, MON, TUE, WED, THU, FRI, SAT};

int main() {
    enum Day today = WED;

    printf("WED value = %d\n", today); // 3
    return 0;
}

7) typedef Keyword

  • typedef creates an alias (new name) for existing data type.
  • Makes code cleaner (especially for structs and pointers).
#include <stdio.h>

typedef unsigned long int ULI;

typedef struct {
    int id;
    char name[30];
} Student;

int main() {
    ULI big = 123456UL;
    Student s = {1, "Sourav"};

    printf("big = %lu\n", big);
    printf("Student: %d %s\n", s.id, s.name);

    return 0;
}

8) Quick Notes

  • struct: separate memory for each member (all values stored).
  • union: shared memory (one value at a time) saves memory.
  • enum: named integer constants for readability.
  • typedef: creates type alias to simplify declarations.
⬅ Previous Next ➡