UNIT V
USER DEFINED DATA TYPES & FILE HANDLING
Syllabus:
User-defined data types: Structure and Union, Dynamic memory allocation.
Introduction to file concepts, File handling operations.
User-Defined Data Types
User-defined data types allow programmers to define their own data types to store
heterogeneous data efficiently.
Types of User-Defined Data Types
Structure (struct)
Union (union)
Enumeration (enum)
Typedef (typedef)
Structure
Definition
A structure is a collection of variables of different data types grouped under a
single name.
Syntax
struct structure_name {
data_type member1;
data_type member2;
};
Example
struct Employee {
int id;
char name[20];
float salary;
};
Declaration of Structure Variables:
struct Employee e1, e2;
Accessing Structure Members:
e1.id = 101;
printf("%d", e1.id);
Initialization:
Structure initialization means assigning values to structure members at the time of
declaration.
It helps in setting initial values easily and improves code readability.
, struct Student {
int roll;
char name[20];
float marks;
};
struct Student s1 = {101, "Anil", 85.5};
Structure Input Using scanf():
struct Student {
int roll;
char name[20];
float marks;
};
Void main(){
struct Student s1;
scanf("%d", &s1.roll);
scanf("%s", s1.name);
scanf("%f", &s1.marks);
printf("%d", s1.roll);
printf("%s", s1.name);
printf("%f", s1.marks);
}
Memory Allocation in Structures:
Memory allocation in structures refers to how memory is reserved for structure
variables and their members.
Memory is allocated when a structure variable is declared, not when the structure is
defined.
struct Student
{
int roll;
char name[20];
float marks;
};
struct Student s1;
Memory Allocation
Memory is allocated only when s1 is declared
Total memory = sum of sizes of all members
Example:
int → 4 bytes
char[20] → 20 bytes
float → 4 bytes
Total = 28 bytes
Example program:
USER DEFINED DATA TYPES & FILE HANDLING
Syllabus:
User-defined data types: Structure and Union, Dynamic memory allocation.
Introduction to file concepts, File handling operations.
User-Defined Data Types
User-defined data types allow programmers to define their own data types to store
heterogeneous data efficiently.
Types of User-Defined Data Types
Structure (struct)
Union (union)
Enumeration (enum)
Typedef (typedef)
Structure
Definition
A structure is a collection of variables of different data types grouped under a
single name.
Syntax
struct structure_name {
data_type member1;
data_type member2;
};
Example
struct Employee {
int id;
char name[20];
float salary;
};
Declaration of Structure Variables:
struct Employee e1, e2;
Accessing Structure Members:
e1.id = 101;
printf("%d", e1.id);
Initialization:
Structure initialization means assigning values to structure members at the time of
declaration.
It helps in setting initial values easily and improves code readability.
, struct Student {
int roll;
char name[20];
float marks;
};
struct Student s1 = {101, "Anil", 85.5};
Structure Input Using scanf():
struct Student {
int roll;
char name[20];
float marks;
};
Void main(){
struct Student s1;
scanf("%d", &s1.roll);
scanf("%s", s1.name);
scanf("%f", &s1.marks);
printf("%d", s1.roll);
printf("%s", s1.name);
printf("%f", s1.marks);
}
Memory Allocation in Structures:
Memory allocation in structures refers to how memory is reserved for structure
variables and their members.
Memory is allocated when a structure variable is declared, not when the structure is
defined.
struct Student
{
int roll;
char name[20];
float marks;
};
struct Student s1;
Memory Allocation
Memory is allocated only when s1 is declared
Total memory = sum of sizes of all members
Example:
int → 4 bytes
char[20] → 20 bytes
float → 4 bytes
Total = 28 bytes
Example program: