⚙️ C programming basics · Class 10 · Lesson 6 of 18 · Free
C Console Project – Build a Student Records Manager (structs, files and menus)
⏱ About 50 minutes · ▶ run real C here — nothing to install · no sign-in
In this lesson you will
- Plan a console project as data (structs), actions (functions) and storage (a file)
- Build a menu with a loop and switch, and read user input safely
- Split a C program into a header file, a .c file and main.c
Your class teacher still keeps marks in a notebook, and finding one student's marks means flipping pages. In this lesson you build a small program she could really use: a menu to add, list, find and save student records. It uses everything from Lessons 1–5 together.
How do you make a menu in C with a loop and switch?
Every console app follows the same plan: show a menu, read a choice, do the job, repeat until the user picks Exit. A do-while loop fits, because the menu must show at least once.
switch (choice) jumps straight to the matching case. break ends the case; without it, C keeps running into the next case. default catches every other value.
Here the "inputs" box plays the user. It types 1, 1, 2, 7 and 0.
Try it: change the inputs to 1, 1, 1, 2, 0 and run again.
1 Mark present 2 Show count 0 Exit Choice: 1 Marked. 1 Mark present 2 Show count 0 Exit Choice: 1 Marked. 1 Mark present 2 Show count 0 Exit Choice: 2 Present today: 2 1 Mark present 2 Show count 0 Exit Choice: 7 Unknown choice 7 1 Mark present 2 Show count 0 Exit Choice: 0 Bye!
💡 If scanf cannot read a number (or the input ends), the loop stops instead of running forever.
How do you read numbers and names safely in a C program?
Mixing scanf and fgets causes a famous bug. scanf("%d") reads the number but leaves the Enter key ('\n') behind. The next fgets reads that empty line, so the name is skipped.
A safer habit: read EVERY line with fgets, then take the number out with sscanf. sscanf is scanf that reads from a string, and it returns how many values it filled.
read_int keeps asking until the user types a number. Look at the inputs: "abc" is rejected politely.
Try it: change "abc" to "ninety" or to "95" and run again.
Roll number: 7 Name: Riya Sharma Marks: abc Please type a number. Marks: 91 Saved roll 7, Riya Sharma, 91 marks
How do you add, list and find records in an array of structs?
The data is an array of Student structs (Lesson 4) plus a count of how many are filled. Each action is its own small function.
add_student checks that the array is not full before writing. It takes int *count (Lesson 2) so it can increase the caller's count. find_by_roll returns a pointer to the record, or NULL if no student matches.
Try it: add a third student and then search for that roll number.
Found: Zoya Khan has 94 marks Records: 2 of 3
💡 #define MAX 3 makes a named constant. Change it in one place and the whole program follows.
How do you save and load records in a text file in C?
We save one student per line as roll,marks,name. The name goes last because it may contain spaces.
To read it back, fscanf uses %39[^\n]. This means "read up to 39 characters that are not a newline", so the full name with spaces is read. The loop also stops when the array is full.
This box comes with students.txt. The program loads it, adds one record, saves, and shows the new file.
Try it: add a line like 12,81,Meera Nair to students.txt and run again.
4,76,Aman Verma 9,94,Zoya Khan
Loaded 2 students Saved 3 students. The file now has: 4,76,Aman Verma 9,94,Zoya Khan 15,68,Kabir Singh
💡 (Student) {15, "Kabir Singh", 68} builds a whole struct value in one line.
How do you split a C project into .h and .c files?
Big programs are split into files. records.h is a header: it holds the struct and the list of function declarations (prototypes). records.c holds the function bodies. main.c holds only the menu.
Both .c files write #include "records.h", so they agree on the struct. The #ifndef lines stop the header from being included twice. The compiler builds both .c files and links them into one program.
This is the full project. It loads students.txt, then the inputs list, add, find, save and exit. records.h and records.c are shown above main.c — read them first.
Try it: change the inputs to add yourself as a student, then list again before saving.
#ifndef RECORDS_H
#define RECORDS_H
#define MAX_STUDENTS 50
typedef struct {
int roll;
char name[40];
int marks;
} Student;
int read_int(const char *prompt);
void read_line(const char *prompt, char *buf, int size);
int load_records(const char *file, Student list[], int max);
int save_records(const char *file, const Student list[], int count);
void print_records(const Student list[], int count);
const Student *find_by_roll(const Student list[], int count, int roll);
#endif
#include <stdio.h>
#include <string.h>
#include "records.h"
int read_int(const char *prompt) {
char line[40];
int value;
while (1) {
printf("%s", prompt);
if (fgets(line, sizeof(line), stdin) == NULL) {
return 0;
}
if (sscanf(line, "%d", &value) == 1) {
return value;
}
printf("Please type a number.\n");
}
}
void read_line(const char *prompt, char *buf, int size) {
printf("%s", prompt);
if (fgets(buf, size, stdin) == NULL) {
buf[0] = '\0';
return;
}
buf[strcspn(buf, "\n")] = '\0';
}
int load_records(const char *file, Student list[], int max) {
FILE *f = fopen(file, "r");
int n = 0;
if (f == NULL) {
return 0;
}
while (n < max && fscanf(f, "%d,%d,%39[^\n]", &list[n].roll, &list[n].marks, list[n].name) == 3) {
n++;
}
fclose(f);
return n;
}
int save_records(const char *file, const Student list[], int count) {
FILE *f = fopen(file, "w");
if (f == NULL) {
return 0;
}
for (int i = 0; i < count; i++) {
fprintf(f, "%d,%d,%s\n", list[i].roll, list[i].marks, list[i].name);
}
fclose(f);
return count;
}
void print_records(const Student list[], int count) {
for (int i = 0; i < count; i++) {
printf("%3d %-16s %3d\n", list[i].roll, list[i].name, list[i].marks);
}
}
const Student *find_by_roll(const Student list[], int count, int roll) {
for (int i = 0; i < count; i++) {
if (list[i].roll == roll) {
return &list[i];
}
}
return NULL;
}
4,76,Aman Verma 9,94,Zoya Khan
Loaded 2 students 1 List 2 Add 3 Find 4 Save 0 Exit Choice: 1 4 Aman Verma 76 9 Zoya Khan 94 1 List 2 Add 3 Find 4 Save 0 Exit Choice: 2 Roll: 12 Name: Meera Nair Marks: 88 1 List 2 Add 3 Find 4 Save 0 Exit Choice: 3 Roll: 12 Meera Nair has 88 marks 1 List 2 Add 3 Find 4 Save 0 Exit Choice: 4 Saved 3 students 1 List 2 Add 3 Find 4 Save 0 Exit Choice: 0 Bye!
💡 main uses if / else if here because choice 2 also checks the count. A switch would work too; a case that declares a variable then needs its own { } block.
🤔 Predict the output
Think like C: read the code and choose what it prints. You get the reason straight away.
Question 1. What does this print?
int c = 2;
switch (c) {
case 1:
printf("A");
case 2:
printf("B");
case 3:
printf("C");
break;
default:
printf("D");
}
printf("\n");Question 2. What does this print?
char line[] = "12abc";
int n = 0;
int ok = sscanf(line, "%d", &n);
printf("%d %d\n", ok, n);Question 3. What does this print?
char line[] = "7,88,Riya Sharma";
int roll, marks;
char name[40];
sscanf(line, "%d,%d,%39[^\n]", &roll, &marks, name);
printf("%s|%d\n", name, marks + roll);🧩 Your challenge
Build a mini library desk. Books are in an array of structs. The menu (read with read_int) has 1 List, 2 Issue, 3 Return, 0 Exit. Issue sets available to 0 (or says it is already issued); Return sets it to 1. On Exit, save every book to books.txt as id,available,title and print the file. With the given inputs the output ends with: Saved. books.txt: 101,1,Wings of Fire 102,0,Godan 103,1,Gitanjali
1 List 2 Issue 3 Return 0 Exit: 2 Book id: 102 Issued: Godan 1 List 2 Issue 3 Return 0 Exit: 2 Book id: 102 Godan is already issued 1 List 2 Issue 3 Return 0 Exit: 3 Book id: 103 Returned: Gitanjali 1 List 2 Issue 3 Return 0 Exit: 1 101 Wings of Fire in 102 Godan out 103 Gitanjali in 1 List 2 Issue 3 Return 0 Exit: 0 Saved. books.txt: 101,1,Wings of Fire 102,0,Godan 103,1,Gitanjali
⚠️ Common mistakes (and how to fix them)
❌ Wrong
int choice = 1;
switch (choice) {
case 1:
printf("Listing students\n");
case 2:
printf("Adding a student\n");
break;
}Compiles and runs, but prints: Listing students ⏎ Adding a student ⏎
✅ Right
int choice = 1;
switch (choice) {
case 1:
printf("Listing students\n");
break;
case 2:
printf("Adding a student\n");
break;
}Without break, case 1 falls through and also runs case 2. End every case with break unless you really want fall-through.
❌ Wrong
char cmd[] = "add";
switch (cmd) {
case "add":
printf("Adding\n");
break;
}Line 2: error: statement requires expression of integer type ('char[4]' invalid)
✅ Right
char cmd[] = "add";
if (strcmp(cmd, "add") == 0) {
printf("Adding\n");
}switch works only with whole numbers (and chars). For text commands, compare with strcmp in an if / else if chain.
❌ Wrong
FILE *f = fopen("s.txt", "w");
fprintf(f, "1,90,Riya Sharma\n2,85,Aman Gupta\n");
fclose(f);
int roll, marks, n = 0;
char name[40];
f = fopen("s.txt", "r");
while (fscanf(f, "%d,%d,%39s", &roll, &marks, name) == 3) {
n++;
}
fclose(f);
printf("Loaded %d students\n", n);Compiles and runs, but prints: Loaded 1 students ⏎
✅ Right
FILE *f = fopen("s.txt", "w");
fprintf(f, "1,90,Riya Sharma\n2,85,Aman Gupta\n");
fclose(f);
int roll, marks, n = 0;
char name[40];
f = fopen("s.txt", "r");
while (fscanf(f, "%d,%d,%39[^\n]", &roll, &marks, name) == 3) {
n++;
}
fclose(f);
printf("Loaded %d students\n", n);%s stops at the space in "Riya Sharma". The next fscanf then finds "Sharma" where it wants a number and gives up. Use %39[^\n] for names with spaces.
📌 Remember
- Plan a project as data (structs), actions (functions) and storage (a file).
- A do-while loop with switch (or if / else if) makes a menu; end each case with break.
- Read every line with fgets and pull numbers out with sscanf to avoid leftover-newline bugs.
- Save one record per line with fprintf, and load with fscanf using %[^\n] for names with spaces.
- Split bigger programs into a header (.h) with declarations and .c files with the code.
Finished the quiz and the challenge?
Questions students ask
What are good C projects for beginners?
Small console programs that use structs, files and a menu: a student records manager, a library desk, a kirana shop's stock register, a quiz game or an expense tracker. Start small and add one feature at a time.
Why does fgets skip input after scanf in C?
scanf("%d") leaves the Enter key ('\n') in the input. fgets then reads that empty line at once. Read every line with fgets and use sscanf to get numbers, as in this lesson.
What goes in a .h file and what goes in a .c file?
A header (.h) holds what other files need to know: struct types, #define constants and function declarations. The .c file holds the function bodies. Each .c file that uses the functions includes the header.
What is the difference between switch and if-else in C?
switch compares one whole-number value against fixed cases and is neat for menus. if-else can test any condition, like ranges or strings with strcmp. Both are fine; choose the one that reads more clearly.