C Mini Project: Student Records Manager
In this project you will build a small but complete program that manages student records: adding students, listing them in a table, computing statistics, searching, sorting, removing, and finally saving everything to a file and loading it back. It combines most of what you have learned — structs, arrays, strings, functions, pointers, enums, qsort and file I/O.
We build it in five steps. Each playground is a complete program you can run and modify; each step reuses the code of the previous one and adds a feature.
| Step | Feature | Concepts used |
|---|---|---|
| 1 | A Student record |
struct, typedef, const char *, printf widths |
| 2 | A class register with add & list | arrays of structs, pointers, return codes |
| 3 | Statistics and search | loops, returning pointers, NULL |
| 4 | Sorting and removing | qsort, comparison functions, shifting arrays |
| 5 | Saving and loading | fopen, fprintf, fgets, sscanf |
Step 1: The student record
A student has an ID, a name and a score from 0 to 100. A fixed-size char array holds the name inside the struct, so every record is self-contained. The letter grade is derived from the score rather than stored, so it can never get out of sync.
#include <stdio.h>
#include <string.h>
#define NAME_LEN 32
typedef struct {
int id;
char name[NAME_LEN];
double score;
} Student;
char grade_of(double score) {
if (score >= 90) return 'A';
if (score >= 80) return 'B';
if (score >= 70) return 'C';
if (score >= 60) return 'D';
return 'F';
}
void print_student(const Student *s) {
printf("%4d %-16s %6.1f %c\n", s->id, s->name, s->score, grade_of(s->score));
}
int main(void) {
Student s = {.id = 101, .score = 87.5};
strncpy(s.name, "Amara Okafor", NAME_LEN - 1);
s.name[NAME_LEN - 1] = '\0';
print_student(&s);
printf("sizeof(Student) = %zu bytes\n", sizeof(Student));
return 0;
} 101 Amara Okafor 87.5 B
sizeof(Student) = 48 bytes
strncpy copies at most NAME_LEN - 1 characters, and we set the last byte to '\0' ourselves, so a very long name is cut off instead of overflowing the array. print_student takes a const Student *: passing a pointer avoids copying 48 bytes, and const promises not to change the record.
Step 2: A class register
Next we need a collection. A Register struct bundles a fixed-capacity array with a count of how many slots are in use. add_student returns an enum status code, the usual C way of reporting errors:
#include <stdio.h>
#include <string.h>
#define NAME_LEN 32
#define MAX_STUDENTS 50
typedef struct {
int id;
char name[NAME_LEN];
double score;
} Student;
typedef struct {
Student items[MAX_STUDENTS];
int count;
} Register;
typedef enum { ADD_OK, ADD_FULL, ADD_BAD_SCORE, ADD_DUPLICATE } AddResult;
char grade_of(double score) {
if (score >= 90) return 'A';
if (score >= 80) return 'B';
if (score >= 70) return 'C';
if (score >= 60) return 'D';
return 'F';
}
AddResult add_student(Register *r, int id, const char *name, double score) {
if (r->count == MAX_STUDENTS) return ADD_FULL;
if (score < 0 || score > 100) return ADD_BAD_SCORE;
for (int i = 0; i < r->count; i++) {
if (r->items[i].id == id) return ADD_DUPLICATE;
}
Student *s = &r->items[r->count];
s->id = id;
snprintf(s->name, NAME_LEN, "%s", name); // safe copy, always terminated
s->score = score;
r->count++;
return ADD_OK;
}
void list_students(const Register *r) {
printf(" ID Name Score Grade\n");
printf("---- ---------------- ------ -----\n");
for (int i = 0; i < r->count; i++) {
const Student *s = &r->items[i];
printf("%4d %-16s %6.1f %c\n", s->id, s->name, s->score, grade_of(s->score));
}
printf("(%d students)\n", r->count);
}
int main(void) {
static Register reg; // static: zero-initialised, count starts at 0
add_student(®, 101, "Amara Okafor", 87.5);
add_student(®, 102, "Ben Fischer", 72.0);
add_student(®, 103, "Chen Jing", 94.0);
add_student(®, 104, "Diego Ramos", 58.5);
if (add_student(®, 102, "Someone Else", 80) == ADD_DUPLICATE) {
printf("Rejected: ID 102 already exists\n");
}
if (add_student(®, 105, "Eva Novak", 120) == ADD_BAD_SCORE) {
printf("Rejected: score 120 is out of range\n");
}
list_students(®);
return 0;
}Rejected: ID 102 already exists
Rejected: score 120 is out of range
ID Name Score Grade
---- ---------------- ------ -----
101 Amara Okafor 87.5 B
102 Ben Fischer 72.0 C
103 Chen Jing 94.0 A
104 Diego Ramos 58.5 F
(4 students)
Validating input inside add_student means the rest of the program can trust that every stored record is valid.
Step 3: Statistics and search
With the data in place, we can answer questions about it. find_by_id returns a pointer to the matching record — or NULL when there is none — so the caller can both read and update it. print_stats counts grades with an array indexed by grade - 'A':
#include <stdio.h>
#include <string.h>
#define NAME_LEN 32
#define MAX_STUDENTS 50
typedef struct { int id; char name[NAME_LEN]; double score; } Student;
typedef struct { Student items[MAX_STUDENTS]; int count; } Register;
char grade_of(double score) {
if (score >= 90) return 'A';
if (score >= 80) return 'B';
if (score >= 70) return 'C';
if (score >= 60) return 'D';
return 'F';
}
void add_student(Register *r, int id, const char *name, double score) {
if (r->count == MAX_STUDENTS) return;
Student *s = &r->items[r->count++];
s->id = id;
snprintf(s->name, NAME_LEN, "%s", name);
s->score = score;
}
Student *find_by_id(Register *r, int id) {
for (int i = 0; i < r->count; i++) {
if (r->items[i].id == id) return &r->items[i];
}
return NULL;
}
void print_stats(const Register *r) {
if (r->count == 0) {
printf("No students.\n");
return;
}
double sum = 0;
const Student *best = &r->items[0];
int per_grade[6] = {0}; // A B C D E F (E unused)
for (int i = 0; i < r->count; i++) {
const Student *s = &r->items[i];
sum += s->score;
if (s->score > best->score) best = s;
per_grade[grade_of(s->score) - 'A']++;
}
printf("Average score: %.2f\n", sum / r->count);
printf("Top student: %s (%.1f)\n", best->name, best->score);
printf("Grades: A=%d B=%d C=%d D=%d F=%d\n",
per_grade[0], per_grade[1], per_grade[2], per_grade[3], per_grade[5]);
}
int main(void) {
static Register reg;
add_student(®, 101, "Amara Okafor", 87.5);
add_student(®, 102, "Ben Fischer", 72.0);
add_student(®, 103, "Chen Jing", 94.0);
add_student(®, 104, "Diego Ramos", 58.5);
add_student(®, 105, "Eva Novak", 81.0);
print_stats(®);
int wanted[] = {104, 999};
for (int i = 0; i < 2; i++) {
Student *s = find_by_id(®, wanted[i]);
if (s != NULL) {
printf("Found %d: %s, %.1f\n", s->id, s->name, s->score);
s->score += 5; // a re-take improved the score
printf("Updated %s to %.1f (grade %c)\n", s->name, s->score, grade_of(s->score));
} else {
printf("No student with ID %d\n", wanted[i]);
}
}
print_stats(®);
return 0;
}Average score: 78.60
Top student: Chen Jing (94.0)
Grades: A=1 B=2 C=1 D=0 F=1
Found 104: Diego Ramos, 58.5
Updated Diego Ramos to 63.5 (grade D)
No student with ID 999
Average score: 79.60
Top student: Chen Jing (94.0)
Grades: A=1 B=2 C=1 D=1 F=0
Because find_by_id returns a pointer into the register, changing s->score updates the stored record directly — no copying back needed.
Step 4: Sorting and removing
The standard library's qsort (from <stdlib.h>) can sort any array. You give it the array, the number of elements, the size of one element, and a comparison function that receives two const void * pointers and returns negative, zero or positive. Removing a student shifts the following records one slot to the left with memmove:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAME_LEN 32
#define MAX_STUDENTS 50
typedef struct { int id; char name[NAME_LEN]; double score; } Student;
typedef struct { Student items[MAX_STUDENTS]; int count; } Register;
void add_student(Register *r, int id, const char *name, double score) {
if (r->count == MAX_STUDENTS) return;
Student *s = &r->items[r->count++];
s->id = id;
snprintf(s->name, NAME_LEN, "%s", name);
s->score = score;
}
void list_students(const Register *r, const char *title) {
printf("%s:\n", title);
for (int i = 0; i < r->count; i++) {
printf(" %4d %-14s %5.1f\n", r->items[i].id, r->items[i].name, r->items[i].score);
}
}
int by_score_desc(const void *a, const void *b) {
const Student *x = a, *y = b;
if (x->score < y->score) return 1;
if (x->score > y->score) return -1;
return x->id - y->id; // tie-break: lower ID first
}
int by_name(const void *a, const void *b) {
const Student *x = a, *y = b;
return strcmp(x->name, y->name);
}
int remove_student(Register *r, int id) {
for (int i = 0; i < r->count; i++) {
if (r->items[i].id == id) {
memmove(&r->items[i], &r->items[i + 1],
(size_t)(r->count - i - 1) * sizeof(Student));
r->count--;
return 1;
}
}
return 0;
}
int main(void) {
static Register reg;
add_student(®, 101, "Amara Okafor", 87.5);
add_student(®, 102, "Ben Fischer", 72.0);
add_student(®, 103, "Chen Jing", 94.0);
add_student(®, 104, "Diego Ramos", 63.5);
add_student(®, 105, "Eva Novak", 72.0);
qsort(reg.items, reg.count, sizeof(Student), by_score_desc);
list_students(®, "Ranking");
qsort(reg.items, reg.count, sizeof(Student), by_name);
list_students(®, "Alphabetical");
printf("remove 102: %s\n", remove_student(®, 102) ? "done" : "not found");
printf("remove 102: %s\n", remove_student(®, 102) ? "done" : "not found");
list_students(®, "After removal");
return 0;
}Ranking:
103 Chen Jing 94.0
101 Amara Okafor 87.5
102 Ben Fischer 72.0
105 Eva Novak 72.0
104 Diego Ramos 63.5
Alphabetical:
101 Amara Okafor 87.5
102 Ben Fischer 72.0
103 Chen Jing 94.0
104 Diego Ramos 63.5
105 Eva Novak 72.0
remove 102: done
remove 102: not found
After removal:
101 Amara Okafor 87.5
103 Chen Jing 94.0
104 Diego Ramos 63.5
105 Eva Novak 72.0
Comparing doubles with < and > instead of returning x->score - y->score matters: the difference would be converted to int, so 72.0 vs 72.4 would wrongly count as equal. memmove (rather than memcpy) is required because the source and destination regions overlap.
Step 5: Saving and loading
Finally, records should survive after the program ends. We save one student per line in a simple comma-separated format, then load the file into a fresh register. Loading reads each line with fgets and parses it with sscanf; the %31[^,] conversion reads up to 31 characters that are not commas — perfect for names containing spaces:
#include <stdio.h>
#include <string.h>
#define NAME_LEN 32
#define MAX_STUDENTS 50
typedef struct { int id; char name[NAME_LEN]; double score; } Student;
typedef struct { Student items[MAX_STUDENTS]; int count; } Register;
void add_student(Register *r, int id, const char *name, double score) {
if (r->count == MAX_STUDENTS) return;
Student *s = &r->items[r->count++];
s->id = id;
snprintf(s->name, NAME_LEN, "%s", name);
s->score = score;
}
int save_register(const Register *r, const char *path) {
FILE *fp = fopen(path, "w");
if (fp == NULL) return 0;
fprintf(fp, "id,name,score\n"); // header line
for (int i = 0; i < r->count; i++) {
const Student *s = &r->items[i];
fprintf(fp, "%d,%s,%.1f\n", s->id, s->name, s->score);
}
fclose(fp);
return 1;
}
int load_register(Register *r, const char *path) {
FILE *fp = fopen(path, "r");
if (fp == NULL) return -1;
char line[128];
int skipped = 0;
r->count = 0;
if (fgets(line, sizeof line, fp) == NULL) { // skip the header
fclose(fp);
return 0; // empty file
}
while (fgets(line, sizeof line, fp) != NULL) {
int id;
char name[NAME_LEN];
double score;
if (sscanf(line, "%d,%31[^,],%lf", &id, name, &score) == 3) {
add_student(r, id, name, score);
} else {
skipped++;
}
}
fclose(fp);
return skipped;
}
int main(void) {
static Register original, loaded;
add_student(&original, 101, "Amara Okafor", 87.5);
add_student(&original, 103, "Chen Jing", 94.0);
add_student(&original, 105, "Eva Novak", 72.0);
if (!save_register(&original, "students.csv")) {
printf("could not save\n");
return 1;
}
// Show the file exactly as it was written
FILE *fp = fopen("students.csv", "r");
char line[128];
printf("--- students.csv ---\n");
while (fp != NULL && fgets(line, sizeof line, fp) != NULL) {
printf("%s", line);
}
if (fp != NULL) fclose(fp);
printf("--------------------\n");
// Append a broken line to show that loading is robust
fp = fopen("students.csv", "a");
if (fp != NULL) {
fprintf(fp, "this line is not valid\n");
fclose(fp);
}
int skipped = load_register(&loaded, "students.csv");
printf("Loaded %d students (%d bad line skipped)\n", loaded.count, skipped);
for (int i = 0; i < loaded.count; i++) {
printf(" %d %s %.1f\n", loaded.items[i].id, loaded.items[i].name, loaded.items[i].score);
}
remove("students.csv");
return 0;
}--- students.csv ---
id,name,score
101,Amara Okafor,87.5
103,Chen Jing,94.0
105,Eva Novak,72.0
--------------------
Loaded 3 students (1 bad line skipped)
101 Amara Okafor 87.5
103 Chen Jing 94.0
105 Eva Novak 72.0
The round trip works: the three records come back unchanged, and the invalid line is counted and skipped instead of corrupting the data. Checking the return value of sscanf is what makes the loader robust — a line that does not match the expected format simply yields fewer than 3 conversions.
Where to go from here
You now have a working records manager. Ideas for extending it on your own machine (where you can read the keyboard):
- Add an interactive menu with a
do…whileloop, reading choices withfgets+sscanf. - Replace the fixed-size array with a dynamic array that grows with
realloc. - Split the code into
student.h,student.candmain.c, usingstaticfor private helpers. - Store several scores per student (an array inside the struct) and compute per-subject averages.
- Handle names containing commas by quoting them in the CSV file.
Tip: Grow programs like this one step at a time — compile and test after every feature. Small steps make bugs easy to find, because the mistake is almost always in the code you just added.
Test yourself
Why does find_by_id return a Student * instead of a Student?
- Pointers are always faster
- So the caller can modify the stored record and so it can return NULL when nothing is found
- Structs cannot be returned in C
- To save memory in the file
What must a qsort comparison function return when the first element should come first?
- A positive number
- Zero
- A negative number
- true
Why is memmove used instead of memcpy when removing a record?
- memmove is faster
- The source and destination regions overlap, which memcpy does not support
- memcpy cannot copy structs
- memmove frees the memory