Unit-4
Unit-4
a. Structures
A structure is a collection of variables (of different types) grouped together under one name. It is used to model
real-world entities.
Syntax:
struct StructureName {
dataType1 variable1;
dataType2 variable2;
// Add more variables if needed
};
Example:
struct Student {
int rollNo;
char name[50];
float marks;
};
Usage: You can create variables of the structure type and access its members using the dot (.) operator.
b. Enumerations
An enumeration (enum) is a user-defined type consisting of named integer constants. It is useful when you have a
set of predefined values.
Syntax:
Example:
2. File Handling
File handling allows programs to read from and write data to files on the storage device.
File Operations
1. File Creation
2. File Reading
3. File Writing
4. File Appending
5. Closing a File
Types of Files
1. Text Files: Store data in human-readable format.
2. Binary Files: Store data in binary (machine-readable) format.
3. Open a file using fopen(). Modes: "r" (read), "w" (write), "a" (append), "rb" (read binary), "wb" (write binary),
etc.
File Functions
1. fopen(): Opens a file.
2. fclose(): Closes an opened file.
3. fprintf(): Writes formatted data to a file.