Build a complete Student Management System in C with file handling, CRUD operations, searching, sorting, and menu-driven interface. Full source code included.
This project implements a complete Student Management System using C programming. It demonstrates file handling, structures, dynamic memory, and modular programming. Students can be added, viewed, searched, updated, and deleted – with all data persisted to a file.
Project Features
- Add new student records
- Display all students
- Search by roll number or name
- Update student information
- Delete student records
- Sort by name or marks
- Calculate class statistics (average, topper)
- File-based persistent storage
Data Structure Design
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_NAME 50
#define MAX_STUDENTS 100
#define FILENAME "students.dat"
typedef struct {
int rollNo;
char name[MAX_NAME];
float marks;
char grade;
} Student;
Complete Source Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME 50
#define FILENAME "students.dat"
typedef struct {
int rollNo;
char name[MAX_NAME];
float marks;
char grade;
} Student;
// Calculate grade based on marks
char calculateGrade(float marks) {
if (marks >= 90) return 'A';
if (marks >= 80) return 'B';
if (marks >= 70) return 'C';
if (marks >= 60) return 'D';
return 'F';
}
// Add a new student
void addStudent() {
FILE *fp = fopen(FILENAME, "ab");
if (!fp) { perror("Error opening file"); return; }
Student s;
printf("\n--- Add New Student ---\n");
printf("Enter Roll Number: ");
scanf("%d", &s.rollNo);
getchar(); // consume newline
printf("Enter Name: ");
fgets(s.name, MAX_NAME, stdin);
s.name[strcspn(s.name, "\n")] = '\0'; // remove newline
printf("Enter Marks (0-100): ");
scanf("%f", &s.marks);
s.grade = calculateGrade(s.marks);
fwrite(&s, sizeof(Student), 1, fp);
fclose(fp);
printf("Student added successfully! Grade: %c\n", s.grade);
}
// Display all students
void displayAll() {
FILE *fp = fopen(FILENAME, "rb");
if (!fp) { printf("No records found.\n"); return; }
Student s;
int count = 0;
printf("\n%-8s %-30s %-8s %-6s\n", "Roll No", "Name", "Marks", "Grade");
printf("------------------------------------------------------\n");
while (fread(&s, sizeof(Student), 1, fp) == 1) {
printf("%-8d %-30s %-8.1f %-6c\n", s.rollNo, s.name, s.marks, s.grade);
count++;
}
printf("------------------------------------------------------\n");
printf("Total Students: %d\n", count);
fclose(fp);
}
// Search by roll number
void searchByRoll() {
int roll;
printf("Enter Roll Number to search: ");
scanf("%d", &roll);
FILE *fp = fopen(FILENAME, "rb");
if (!fp) { printf("No records found.\n"); return; }
Student s;
int found = 0;
while (fread(&s, sizeof(Student), 1, fp) == 1) {
if (s.rollNo == roll) {
printf("\n--- Student Found ---\n");
printf("Roll No: %d\n", s.rollNo);
printf("Name: %s\n", s.name);
printf("Marks: %.1f\n", s.marks);
printf("Grade: %c\n", s.grade);
found = 1;
break;
}
}
if (!found) printf("Student with Roll No %d not found.\n", roll);
fclose(fp);
}
// Update student record
void updateStudent() {
int roll;
printf("Enter Roll Number to update: ");
scanf("%d", &roll);
FILE *fp = fopen(FILENAME, "rb+");
if (!fp) { printf("No records found.\n"); return; }
Student s;
int found = 0;
while (fread(&s, sizeof(Student), 1, fp) == 1) {
if (s.rollNo == roll) {
printf("Current Name: %s\n", s.name);
printf("Current Marks: %.1f\n", s.marks);
getchar();
printf("Enter New Name (or press Enter to keep): ");
char newName[MAX_NAME];
fgets(newName, MAX_NAME, stdin);
newName[strcspn(newName, "\n")] = '\0';
if (strlen(newName) > 0) strcpy(s.name, newName);
printf("Enter New Marks (or -1 to keep): ");
float newMarks;
scanf("%f", &newMarks);
if (newMarks >= 0) {
s.marks = newMarks;
s.grade = calculateGrade(s.marks);
}
fseek(fp, -(long)sizeof(Student), SEEK_CUR);
fwrite(&s, sizeof(Student), 1, fp);
printf("Record updated successfully!\n");
found = 1;
break;
}
}
if (!found) printf("Student with Roll No %d not found.\n", roll);
fclose(fp);
}
// Delete student record
void deleteStudent() {
int roll;
printf("Enter Roll Number to delete: ");
scanf("%d", &roll);
FILE *fp = fopen(FILENAME, "rb");
FILE *temp = fopen("temp.dat", "wb");
if (!fp || !temp) { printf("Error!\n"); return; }
Student s;
int found = 0;
while (fread(&s, sizeof(Student), 1, fp) == 1) {
if (s.rollNo == roll) {
found = 1;
printf("Deleted: %s (Roll No: %d)\n", s.name, s.rollNo);
} else {
fwrite(&s, sizeof(Student), 1, temp);
}
}
fclose(fp);
fclose(temp);
remove(FILENAME);
rename("temp.dat", FILENAME);
if (!found) printf("Student not found.\n");
}
// Calculate class statistics
void showStatistics() {
FILE *fp = fopen(FILENAME, "rb");
if (!fp) { printf("No records found.\n"); return; }
Student s, topper;
float total = 0;
int count = 0;
float highest = -1;
while (fread(&s, sizeof(Student), 1, fp) == 1) {
total += s.marks;
count++;
if (s.marks > highest) {
highest = s.marks;
topper = s;
}
}
fclose(fp);
if (count == 0) { printf("No students.\n"); return; }
printf("\n--- Class Statistics ---\n");
printf("Total Students: %d\n", count);
printf("Class Average: %.2f\n", total / count);
printf("Highest Marks: %.1f\n", highest);
printf("Topper: %s (Roll No: %d)\n", topper.name, topper.rollNo);
}
// Main menu
void showMenu() {
printf("\n========================================\n");
printf(" STUDENT MANAGEMENT SYSTEM\n");
printf("========================================\n");
printf("1. Add Student\n");
printf("2. Display All Students\n");
printf("3. Search Student\n");
printf("4. Update Student\n");
printf("5. Delete Student\n");
printf("6. Class Statistics\n");
printf("7. Exit\n");
printf("========================================\n");
printf("Enter your choice: ");
}
int main() {
int choice;
while (1) {
showMenu();
scanf("%d", &choice);
switch (choice) {
case 1: addStudent(); break;
case 2: displayAll(); break;
case 3: searchByRoll(); break;
case 4: updateStudent(); break;
case 5: deleteStudent(); break;
case 6: showStatistics(); break;
case 7:
printf("Thank you! Exiting...\n");
return 0;
default:
printf("Invalid choice! Try again.\n");
}
}
return 0;
}
Sample Output
========================================
STUDENT MANAGEMENT SYSTEM
========================================
1. Add Student
2. Display All Students
3. Search Student
4. Update Student
5. Delete Student
6. Class Statistics
7. Exit
========================================
Enter your choice: 1
--- Add New Student ---
Enter Roll Number: 101
Enter Name: Rahul Kumar
Enter Marks (0-100): 85.5
Student added successfully! Grade: B
Enter your choice: 2
Roll No Name Marks Grade
------------------------------------------------------
101 Rahul Kumar 85.5 B
102 Priya Sharma 92.0 A
103 Amit Patel 78.3 C
------------------------------------------------------
Total Students: 3
How to Compile and Run
// Linux/Mac
gcc -o student_mgmt student_management.c
./student_mgmt
// Windows
gcc -o student_mgmt.exe student_management.c
student_mgmt.exe
Project Structure Explanation
| Component | Purpose |
|---|
Student struct | Data model with roll, name, marks, grade |
students.dat | Binary file for persistent storage |
| CRUD functions | Add, Read, Update, Delete operations |
| Menu system | User-friendly console interface |
| Grade calculation | Automatic grading based on marks |
| Statistics | Aggregate calculations on stored data |
Possible Enhancements
- Add sorting (by name, marks, or roll number)
- Implement search by name (partial matching)
- Add multiple subjects with individual marks
- Export data to CSV format
- Add login/password protection
- Implement pagination for large datasets
Interview Questions
Q: Why use binary file mode ("rb"/"wb") instead of text mode? Binary mode preserves the exact struct bytes, making fread/fwrite work correctly. Text mode may translate newlines and corrupt binary data.
Q: How would you handle duplicate roll numbers? Before inserting, search the file for the roll number. If found, reject the insertion or prompt the user.
Q: What's the limitation of this approach? File-based storage with fread/fwrite is simple but not efficient for large datasets. For better performance, use indexed files or a database.
Summary
This Student Management System demonstrates fundamental C concepts including structures, file I/O, menu-driven programs, and data persistence. It's a great portfolio project that showcases your ability to build complete, functional applications in C. The modular design makes it easy to extend with additional features.