FOC Unit V: User Defined Data Types

 


User-Defined Data Types (UDDTs)

In C programming, a User-Defined Data Type is a data type created by the programmer to store multiple values of different data types together.
Common UDDTs:

  • Structure (struct)
  • Union (union)
  • typedef
  • enum


Structure (struct)

Definition

A structure is a user-defined data type that allows you to combine variables of different data types under a single name.

Why structure?

  • To group related data
  • Example: student information → roll number, name, marks

Syntax

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

Declaring Structure Variables

struct Student s1, s2;


Accessing Structure Members

Use dot (.) operator.

s1.roll_no = 1; strcpy(s1.name, "Anisha"); s1.marks = 85.5;


Example

#include <stdio.h> struct Student { int roll; char name[20]; float marks; }; int main() { struct Student s1 = {1, "Anisha", 85.5}; printf("Roll: %d\n", s1.roll); printf("Name: %s\n", s1.name); printf("Marks: %.2f\n", s1.marks); return 0; }

Features of Structure

  • Stores multiple values of different types
  • Each member has a separate memory
  • Total memory = sum of all members
  • Members are accessed using dot (.) operator

Memory Example

For struct Student:

int roll = 4 bytes

char name[20] = 20 bytes

float marks = 4 bytes

Total = 28 bytes

UNION – User Defined Data Type

Definition:

A union is a user-defined data type in C that allows different data types to share the same memory location

Syntax

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

Example:

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


Declaration of Union Variables

union Data d1;

Accessing Union Members

Union members are accessed using the dot (.) operator, same as structure. d1.i = 10; printf("%d", d1.i);

Example: #include <stdio.h> union Data { int i; float f; char c; }; int main() { union Data d; d.i = 10; printf("Integer: %d\n", d.i); d.f = 3.14; printf("Float: %.2f\n", d.f); d.c = 'A'; printf("Character: %c\n", d.c); return 0; }

Difference Between Structure and Union:

How Structure Elements Are Stored in Memory

In a structure, each data member is stored in a separate memory location.

Example
struct Student { int roll_no; // 4 bytes char grade; // 1 byte float marks; // 4 bytes };

Array of Structure Elements

An array of structures is used to store multiple records of the same type.

Syntax

struct structure_name array_name[size];

Example

struct Student {

int roll_no;
char name[20];
float marks;
}; struct Student s[3];


Accessing an Array of Structure Elements

Use array index + dot operator.

s[0].roll_no = 1;

s[0].marks = 85.5;

Array of Structures

#include <stdio.h>

struct Student {
int roll_no;
float marks;
}; int main() {
struct Student s[3];
int i; for(i = 0; i < 3; i++) {
printf("Enter roll no and marks: ");
scanf("%d %f", &s[i].roll_no, &s[i].marks);
} printf("\nStudent Details:\n");
for(i = 0; i < 3; i++) {
printf("Roll No: %d Marks: %.2f\n",
s[i].roll_no, s[i].marks);
} return 0;
}

Memory Layout of Array of Structures

  • Structures are stored in contiguous memory

  • Each array element occupies structure size

Advantages of Array of Structures

  • Easy to manage large data
  • Suitable for database-like applications
  • Improves code organization

Post a Comment

0 Comments