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; .... };


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

Post a Comment

0 Comments