Mini Projects Section: Hands-On C Programming Fun
Last Updated on: 29th Jul 2025 15:55:08 PM
Welcome to the Mini Projects Section, where learning C programming becomes an exciting adventure! This section is designed for real-world applications of C concepts like file handling, structures, and input validation. Here, we present three awesome mini-projects: a Calculator, a Student Record System, and a Contact Book. Each project is interactive, beginner-friendly, and packed with learning opportunities, from basic arithmetic to managing persistent data in files. Below, you’ll find an overview of each project, followed by detailed tutorials with code, sample output, and explanations. Get ready to code, solve problems, and have fun!
Table of Contents
-
Student Record System
-
Calculator
-
Contact Book
Student Record System
Welcome to the Student Record System, a practical and engaging mini-project in C that lets you manage student records like a pro! Imagine running a school’s database, where you can add, view, search, update, or delete student details—all stored in a file for permanence. This project is designed to teach students file handling, structures, and problem-solving in C with a real-world application. It’s interactive (using scanf and fgets), robust (with input validation), and fun (think of it as organizing a school’s roster!). The system stores each student’s admission number, name, and grade, and supports operations like adding, reading, searching, updating, and deleting records. Below, you’ll find the complete code, sample output, and clear explanations to make learning a breeze. Let’s get started!
Project Overview
The Student Record System allows users to:
-
Add a Student: Enter admission number, name, and grade, saved to a file (students.txt).
-
View All Students: Display all records from the file.
-
Search by Admission Number: Find a student’s details by their unique admission number.
-
Update a Record: Modify a student’s name or grade.
-
Delete a Record: Remove a student from the system.
Features:
-
Uses a Student structure to store admission number, name, and grade.
-
Stores data in students.txt for persistence.
-
Validates input (e.g., positive admission numbers, valid grades).
-
Handles multi-word names using fgets.
-
Uses a temporary file for update and delete operations to ensure data integrity.
-
Interactive menu with clear prompts, perfect for beginners and advanced learners.
The Code :
# include <stdio.h>
# include <string.h>
int writedata();
int readdata();
int searchdata(int admNo);
int updatedata(int admNo);
int deletedata(int admNo);
int main()
{
int n, admNo;
char ch;
do
{
printf("\nPress 1 for writing");
printf("\nPress 2 for reading");
printf("\nPress 3 for searching by admission number");
printf("\nPress 4 for updating a record");
printf("\nPress 5 for deleting a record");
printf("\nPress 0 for exit");
scanf("%d", &n);
fflush(stdin);
switch(n)
{
case 1:
writedata();
break;
case 2:
readdata();
break;
case 3:
printf("Enter admission number to search: ");
scanf("%d", &admNo);
searchdata(admNo);
break;
case 4:
printf("Enter admission number to update: ");
scanf("%d", &admNo);
updatedata(admNo);
break;
case 5:
printf("Enter admission number to delete: ");
scanf("%d", &admNo);
deletedata(admNo);
break;
case 0:
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice");
}
printf("\n\nIf you want to continue, press (y/n): ");
fflush(stdin);
scanf(" %c", &ch);
}
while(ch == 'Y' || ch == 'y');
return 0;
}
int readdata()
{
FILE *fp;
int admissionNo;
char name[50];
fp = fopen("test.txt", "r");
if (fp == NULL)
{
printf("Could not open file test.txt\n");
return 1;
}
printf("\nAdmission Records:\n");
while (fscanf(fp, "%d %s", &admissionNo, name) != EOF)
{
printf("Admission No: %d\tName: %s\n", admissionNo, name);
}
fclose(fp);
return 0;
}
int writedata()
{
FILE *fp;
int admissionNo;
char name[50];
fp = fopen("test.txt", "a");
if (fp == NULL)
{
printf("Could not open file test.txt\n");
return 1;
}
printf("\nEnter Admission Number: ");
scanf("%d", &admissionNo);
printf("Enter Name: ");
scanf("%s", name);
fprintf(fp, "%d %s\n", admissionNo, name);
fclose(fp);
return 0;
}
int searchdata(int admNo)
{
FILE *fp = fopen("test.txt", "r");
int admissionNo;
char name[50];
int found = 0;
while (fscanf(fp, "%d %s", &admissionNo, name) != EOF)
{
if (admissionNo == admNo)
{
printf("Record Found: Admission No: %d\tName: %s\n", admissionNo, name);
found = 1;
break;
}
}
fclose(fp);
if (!found) printf("Record not found.\n");
return 0;
}
int updatedata(int admNo)
{
FILE *fp = fopen("test.txt", "r");
FILE *temp = fopen("temp.txt", "w");
int admissionNo;
char name[50], newName[50];
int found = 0;
while (fscanf(fp, "%d %s", &admissionNo, name) != EOF)
{
if (admissionNo == admNo)
{
printf("Enter new name: ");
scanf("%s", newName);
fprintf(temp, "%d %s\n", admissionNo, newName);
found = 1;
}
else
{
fprintf(temp, "%d %s\n", admissionNo, name);
}
}
fclose(fp);
fclose(temp);
remove("test.txt");
rename("temp.txt", "test.txt");
if (found)
printf("Record updated successfully.\n");
else
printf("Record not found.\n");
return 0;
}
int deletedata(int admNo)
{
FILE *fp = fopen("test.txt", "r");
FILE *temp = fopen("temp.txt", "w");
int admissionNo;
char name[50];
int found = 0;
while (fscanf(fp, "%d %s", &admissionNo, name) != EOF)
{
if (admissionNo == admNo)
{
found = 1;
continue; // Skip writing this record (deleting it)
}
fprintf(temp, "%d %s\n", admissionNo, name);
}
fclose(fp);
fclose(temp);
remove("test.txt");
rename("temp.txt", "test.txt");
if (found)
printf("Record deleted successfully.\n");
else
printf("Record not found.\n");
return 0;
With Structure Code :
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// Structure to store student details
struct Student {
int admissionNo;
char name[50];
float grade;
};
// Function prototypes
int writeData();
int readData();
int searchData(int admNo);
int updateData(int admNo);
int deleteData(int admNo);
int validateAdmissionNo(int admNo);
int validateGrade(float grade);
// Main function with interactive menu
int main() {
int choice, admNo;
char ch;
do {
printf("\n=== Student Record System ===\n");
printf("1. Add New Student\n");
printf("2. View All Students\n");
printf("3. Search by Admission Number\n");
printf("4. Update Student Record\n");
printf("5. Delete Student Record\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // Clear newline from input buffer
switch (choice) {
case 1:
writeData();
break;
case 2:
readData();
break;
case 3:
printf("Enter admission number to search: ");
scanf("%d", &admNo);
getchar();
if (validateAdmissionNo(admNo)) {
searchData(admNo);
} else {
printf("Invalid admission number! Must be positive.\n");
}
break;
case 4:
printf("Enter admission number to update: ");
scanf("%d", &admNo);
getchar();
if (validateAdmissionNo(admNo)) {
updateData(admNo);
} else {
printf("Invalid admission number! Must be positive.\n");
}
break;
case 5:
printf("Enter admission number to delete: ");
scanf("%d", &admNo);
getchar();
if (validateAdmissionNo(admNo)) {
deleteData(admNo);
} else {
printf("Invalid admission number! Must be positive.\n");
}
break;
case 0:
printf("Thank you for using the Student Record System!\n");
return 0;
default:
printf("Invalid choice! Please try again.\n");
}
printf("\nContinue? (y/n): ");
scanf("%c", &ch);
getchar(); // Clear newline
} while (ch == 'y' || ch == 'Y');
return 0;
}
// Validate admission number
int validateAdmissionNo(int admNo) {
return admNo > 0;
}
// Validate grade (0 to 100)
int validateGrade(float grade) {
return grade >= 0 && grade <= 100;
}
// Add a new student to the file
int writeData() {
FILE *fp = fopen("students.txt", "a");
if (fp == NULL) {
printf("Error: Could not open students.txt!\n");
return 1;
}
struct Student s;
printf("\nEnter Admission Number: ");
scanf("%d", &s.admissionNo);
getchar();
if (!validateAdmissionNo(s.admissionNo)) {
printf("Invalid admission number! Must be positive.\n");
fclose(fp);
return 1;
}
printf("Enter Name: ");
fgets(s.name, 50, stdin);
s.name[strcspn(s.name, "\n")] = '\0';
printf("Enter Grade (0-100): ");
scanf("%f", &s.grade);
getchar();
if (!validateGrade(s.grade)) {
printf("Invalid grade! Must be between 0 and 100.\n");
fclose(fp);
return 1;
}
fprintf(fp, "%d %s %.2f\n", s.admissionNo, s.name, s.grade);
fclose(fp);
printf("Student added successfully!\n");
return 0;
}
// Read and display all student records
int readData() {
FILE *fp = fopen("students.txt", "r");
if (fp == NULL) {
printf("Error: Could not open students.txt!\n");
return 1;
}
struct Student s;
char name[50];
printf("\n=== Student Records ===\n");
printf("%-15s %-20s %-10s\n", "Admission No", "Name", "Grade");
printf("----------------------------------------\n");
while (fscanf(fp, "%d %49[^\n] %f", &s.admissionNo, name, &s.grade) == 3) {
name[strcspn(name, "\n")] = '\0';
printf("%-15d %-20s %-10.2f\n", s.admissionNo, name, s.grade);
fgetc(fp); // Skip newline
}
fclose(fp);
return 0;
}
// Search for a student by admission number
int searchData(int admNo) {
FILE *fp = fopen("students.txt", "r");
if (fp == NULL) {
printf("Error: Could not open students.txt!\n");
return 1;
}
struct Student s;
char name[50];
int found = 0;
while (fscanf(fp, "%d %49[^\n] %f", &s.admissionNo, name, &s.grade) == 3) {
name[strcspn(name, "\n")] = '\0';
if (s.admissionNo == admNo) {
printf("\nRecord Found:\n");
printf("Admission No: %d\nName: %s\nGrade: %.2f\n", s.admissionNo, name, s.grade);
found = 1;
break;
}
fgetc(fp); // Skip newline
}
fclose(fp);
if (!found) {
printf("Record not found for admission number %d.\n", admNo);
}
return 0;
}
// Update a student’s record
int updateData(int admNo) {
FILE *fp = fopen("students.txt", "r");
FILE *temp = fopen("temp.txt", "w");
if (fp == NULL || temp == NULL) {
printf("Error: Could not open file!\n");
if (fp) fclose(fp);
if (temp) fclose(temp);
return 1;
}
struct Student s;
char name[50], newName[50];
float newGrade;
int found = 0;
while (fscanf(fp, "%d %49[^\n] %f", &s.admissionNo, name, &s.grade) == 3) {
name[strcspn(name, "\n")] = '\0';
if (s.admissionNo == admNo) {
printf("Enter new name: ");
fgets(newName, 50, stdin);
newName[strcspn(newName, "\n")] = '\0';
printf("Enter new grade (0-100): ");
scanf("%f", &newGrade);
getchar();
if (!validateGrade(newGrade)) {
printf("Invalid grade! Must be between 0 and 100.\n");
fclose(fp);
fclose(temp);
remove("temp.txt");
return 1;
}
fprintf(temp, "%d %s %.2f\n", s.admissionNo, newName, newGrade);
found = 1;
} else {
fprintf(temp, "%d %s %.2f\n", s.admissionNo, name, s.grade);
}
fgetc(fp); // Skip newline
}
fclose(fp);
fclose(temp);
remove("students.txt");
rename("temp.txt", "students.txt");
if (found) {
printf("Record updated successfully!\n");
} else {
printf("Record not found for admission number %d.\n", admNo);
}
return 0;
}
// Delete a student’s record
int deleteData(int admNo) {
FILE *fp = fopen("students.txt", "r");
FILE *temp = fopen("temp.txt", "w");
if (fp == NULL || temp == NULL) {
printf("Error: Could not open file!\n");
if (fp) fclose(fp);
if (temp) fclose(temp);
return 1;
}
struct Student s;
char name[50];
int found = 0;
while (fscanf(fp, "%d %49[^\n] %f", &s.admissionNo, name, &s.grade) == 3) {
name[strcspn(name, "\n")] = '\0';
if (s.admissionNo != admNo) {
fprintf(temp, "%d %s %.2f\n", s.admissionNo, name, s.grade);
} else {
found = 1;
}
fgetc(fp); // Skip newline
}
fclose(fp);
fclose(temp);
remove("students.txt");
rename("temp.txt", "students.txt");
if (found) {
printf("Record deleted successfully!\n");
} else {
printf("Record not found for admission number %d.\n", admNo);
}
return 0;
}
Sample Output
Here’s an example interaction with the program:
=== Student Record System ===
1. Add New Student
2. View All Students
3. Search by Admission Number
4. Update Student Record
5. Delete Student Record
0. Exit
Enter your choice: 1
Enter Admission Number: 101
Enter Name: Alice Smith
Enter Grade (0-100): 85.5
Student added successfully!
Continue? (y/n): y
=== Student Record System ===
1. Add New Student
2. View All Students
3. Search by Admission Number
4. Update Student Record
5. Delete Student Record
0. Exit
Enter your choice: 1
Enter Admission Number: 102
Enter Name: Bob Johnson
Enter Grade (0-100): 92.0
Student added successfully!
Continue? (y/n): y
=== Student Record System ===
1. Add New Student
2. View All Students
3. Search by Admission Number
4. Update Student Record
5. Delete Student Record
0. Exit
Enter your choice: 2
=== Student Records ===
Admission No Name Grade
----------------------------------------
101 Alice Smith 85.50
102 Bob Johnson 92.00
Continue? (y/n): y
=== Student Record System ===
1. Add New Student
2. View All Students
3. Search by Admission Number
4. Update Student Record
5. Delete Student Record
0. Exit
Enter your choice: 3
Enter admission number to search: 101
Record Found:
Admission No: 101
Name: Alice Smith
Grade: 85.50
Continue? (y/n): y
=== Student Record System ===
1. Add New Student
2. View All Students
3. Search by Admission Number
4. Update Student Record
5. Delete Student Record
0. Exit
Enter your choice: 4
Enter admission number to update: 101
Enter new name: Alice Brown
Enter new grade (0-100): 88.0
Record updated successfully!
Continue? (y/n): y
=== Student Record System ===
1. Add New Student
2. View All Students
3. Search by Admission Number
4. Update Student Record
5. Delete Student Record
0. Exit
Enter your choice: 5
Enter admission number to delete: 102
Record deleted successfully!
Continue? (y/n): y
=== Student Record System ===
1. Add New Student
2. View All Students
3. Search by Admission Number
4. Update Student Record
5. Delete Student Record
0. Exit
Enter your choice: 2
=== Student Records ===
Admission No Name Grade
----------------------------------------
101 Alice Brown 88.00
Continue? (y/n): n
Thank you for using the Student Record System!
File Content (students.txt after above operations):
101 Alice Brown 88.00
How It Works
1. Program Structure
-
Structure: Student stores admissionNo (integer), name (string), and grade (float).
-
Main Menu: A loop with a switch statement offers five operations or exit. Uses scanf for choice and getchar() to clear the input buffer.
-
File: Stores records in students.txt with format: admissionNo name grade.
2. Key Functions
-
writeData():
-
Opens students.txt in append mode ("a").
-
Takes admission number, name (using fgets for multi-word names), and grade.
-
Validates admission number (> 0) and grade (0–100).
-
Writes to file using fprintf and closes it.
-
-
readData():
-
Opens students.txt in read mode ("r").
-
Reads records with fscanf, handling multi-word names using %[^\n].
-
Displays records in a formatted table.
-
-
searchData(int admNo):
-
Reads the file and searches for a matching admission number.
-
Prints the record if found, or a “not found” message.
-
-
updateData(int admNo):
-
Creates a temporary file (temp.txt) to copy records.
-
Updates the matching record with new name and grade (with validation).
-
Replaces the original file with the updated one.
-
-
deleteData(int admNo):
-
Copies all records except the matching one to temp.txt.
-
Replaces the original file, effectively deleting the record.
-
-
validateAdmissionNo(int) and validateGrade(float):
-
Ensure admission numbers are positive and grades are between 0 and 100.
-
3. Enhancements Over Provided Code
-
Multi-Word Names: Uses fgets with %[^\n] in fscanf to handle names like “Alice Smith”.
-
Input Validation: Checks for valid admission numbers and grades.
-
Formatted Output: Displays records in a neat table for readability.
-
Robust File Handling: Checks for file open failures and closes files properly.
-
User-Friendly: Clear prompts, error messages, and a professional menu layout.
-
Buffer Management: Uses getchar() instead of fflush(stdin) for portability.
4. Why It’s Fun
-
Real-Life Application: Mimics a school’s student database, relatable to students.
-
Interactive: Menu-driven with clear options and feedback.
-
Educational: Teaches file handling, structures, input validation, and error handling.
-
Scalable: Easily extendable (e.g., add sorting or more fields like age).
Tips for Students
-
Check File Operations: Always verify fopen returns a non-NULL pointer.
-
Close Files: Use fclose to avoid memory leaks and ensure data is saved.
-
Validate Inputs: Prevent crashes with checks for valid numbers and grades.
-
Handle Multi-Word Strings: Use fgets and %[^\n] for names with spaces.
-
Use Temporary Files: Safely update or delete records by writing to a temp file.
Why This Project Rocks
-
Practical: Simulates a real-world student management system.
-
Comprehensive: Covers file I/O, structures, and user interaction.
-
Engaging: Interactive menu and clear output make learning fun.
-
Extensible: Add features like sorting by grade or exporting to CSV.