Structures, unions and file handling notes — Unit 5
Free unit-wise study notes on structures, unions and file handling for Programming for Problem Solving in C, Semester 1 of B.Tech — Computer Science & Engineering — key concepts, examples, important questions and a revision checklist for semester exams.
Comprehensive hand-written notes covering composite data types (structures and unions) and file I/O operations. Essential for creating real-world applications that store data persistently.
Notebook — 10 pages
Page 1
Wink Notes
B.Tech CSE — 1st Semester
Programming for Problem Solving in C
— Unit - 5 —
1. Introduction to Structures
Arrays can only hold elements of the same data type. What if we want to store information about a Student? We need a name (string), roll number (int), and marks (float). A Structure allows us to group different data types under a single name.
Defining a Structure
struct Student {
char name[50];
int roll;
float marks;
}; // Semicolon is mandatory here!
int main() {
struct Student s1 = {"Alice", 101, 89.5};
printf("Name: %s", s1.name); // Using the dot operator
return 0;
}
Page 2
Wink Notes
B.Tech CSE — 1st Semester
Programming for Problem Solving in C
— Unit - 5 —
2. Array of Structures
If we need to store data for 60 students, creating 60 individual variables (s1, s2, ...) is impractical. Instead, we create an array where each element is a structure.
Array of Structures Example
struct Student class[60]; // Array of 60 students
// Accessing the 5th student's marks
class[4].marks = 95.0;
Page 3
Wink Notes
B.Tech CSE — 1st Semester
Programming for Problem Solving in C
— Unit - 5 —
3. Pointers to Structures
We can have pointers pointing to structures. When accessing members through a pointer, we must use the arrow operator (`->`) instead of the dot operator (`.`).
Dot (.) vs Arrow (->)
struct Student s1 = {"Bob", 102, 75.5};
struct Student *ptr = &s1;
// These two statements do the exact same thing:
printf("%d", s1.roll);
printf("%d", ptr->roll);
Page 4
Wink Notes
B.Tech CSE — 1st Semester
Programming for Problem Solving in C
— Unit - 5 —
4. Unions
A Union is similar to a structure, but it uses the same memory location for all its members. This means a union can only hold ONE of its members' values at any given time.
Structure vs Union
Feature
Structure
Union
Keyword
struct
union
Memory
Allocates separate memory for each member
Allocates shared memory (size of largest member)
Access
All members can be accessed at any time
Only one member can be accessed at a time
Size
Sum of all member sizes
Size of the largest member
Page 5
Wink Notes
B.Tech CSE — 1st Semester
Programming for Problem Solving in C
— Unit - 5 —
5. Typedef and Enum
⇒Typedef
`typedef` is used to give a new name (alias) to an existing data type. It is often used with structures to avoid typing 'struct' repeatedly.
typedef struct {
int x, y;
} Point;
Point p1; // No need to write 'struct Point p1'
⇒Enumerations (Enum)
`enum` is a user-defined type consisting of named integer constants.
enum Day { SUN, MON, TUE, WED, THU, FRI, SAT };
// SUN = 0, MON = 1, etc.
Page 6
Wink Notes
B.Tech CSE — 1st Semester
Programming for Problem Solving in C
— Unit - 5 —
6. Introduction to File Handling
Variables exist in RAM, which is volatile. When the program ends, the data is lost. File handling allows us to store data permanently on the hard drive.
FILE pointer: A special pointer used to communicate with the file.
Opening a file: fopen()
Closing a file: fclose() - Always close files to prevent data corruption!
char buffer[100];
FILE *fptr = fopen("file.txt", "r");
// Read one word at a time
fscanf(fptr, "%s", buffer);
// Read a whole line (safest way)
fgets(buffer, sizeof(buffer), fptr);
Page 9
Wink Notes
B.Tech CSE — 1st Semester
Programming for Problem Solving in C
— Unit - 5 —
9. Binary Files and EOF
⇒Binary I/O (fread / fwrite)
Used primarily for reading/writing arrays or structures directly to disk in raw memory format.