Welcome back! For this module, we dove into some of the most powerful ways to organize data in C by building a Student Record Management System.
This assignment was all about leveling up how we structured our code. Instead of juggling dozens of loose variables, we learned how to neatly package related information together using Structs, define clear, fixed categories using Enums, and optimize our memory usage by letting different data types share the same space using Unions.
We also got hands-on with Dynamic Memory Allocation (malloc and free). This allowed our programs to create arrays on the fly at runtime, meaning our system could easily adapt to handle however many students we threw at it. It was a fantastic, practical way to master memory management!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* ================= ENUM ================= */
// Define an enum called ProgramType
typedef enum {
UNDERGRADUATE,
POSTGRADUATE
} ProgramType;
/* ================= UNION ================= */
// Define a union called AcademicInfo
typedef union {
float gpa;
int research_hours;
} AcademicInfo;
/* ================= STRUCT ================= */
// Define a struct called Student
typedef struct {
char name[100];
int age;
ProgramType program;
AcademicInfo info;
} Student;
/* ================= FUNCTION PROTOTYPES ================= */
void inputStudent(Student *s);
void displayStudent(Student s);
/* ================= MAIN FUNCTION ================= */
int main() {
int n;
printf("Enter number of students: ");
scanf("%d", &n);
// Dynamically allocate memory for n students
Student *students = (Student *)malloc(n * sizeof(Student));
// Check if memory allocation failed
if (students == NULL) {
printf("Memory allocation failed!\n");
return 1; // Exit program with an error code
}
// Loop to input student data
for (int i = 0; i < n; i++) {
printf("\n--- Student %d ---\n", i + 1);
inputStudent(&students[i]);
}
// Display all student records
printf("\n===== Student Records =====\n");
for (int i = 0; i < n; i++) {
displayStudent(students[i]);
}
// Free allocated memory
free(students);
return 0;
}
/* ================= INPUT FUNCTION ================= */
void inputStudent(Student *s) {
// Input name using the suggested format to capture spaces
printf("Enter name: ");
scanf(" %[^\n]", s->name);
// Input age
printf("Enter age: ");
scanf("%d", &s->age);
// Input program type
int progChoice;
printf("Enter Program (0 for Undergraduate, 1 for Postgraduate): ");
scanf("%d", &progChoice);
s->program = (ProgramType)progChoice;
// Based on program: input GPA or research hours
if (s->program == UNDERGRADUATE) {
printf("Enter GPA: ");
scanf("%f", &s->info.gpa);
} else if (s->program == POSTGRADUATE) {
printf("Enter research hours: ");
scanf("%d", &s->info.research_hours);
} else {
printf("Invalid program selected.\n");
}
}
/* ================= DISPLAY FUNCTION ================= */
void displayStudent(Student s) {
printf("\nName: %s\n", s.name);
printf("Age: %d\n", s.age);
// Display program type and corresponding AcademicInfo
if (s.program == UNDERGRADUATE) {
printf("Program: Undergraduate\n");
printf("GPA: %.2f\n", s.info.gpa);
} else if (s.program == POSTGRADUATE) {
printf("Program: Postgraduate\n");
printf("Research Hours: %d\n", s.info.research_hours);
} else {
printf("Program: Unknown\n");
}
}
Here is what it looks like when you compile and run the program:
Enter number of students: 2
--- Student 1 ---
Enter name: Alice Smith
Enter age: 20
Enter Program (0 for Undergraduate, 1 for Postgraduate): 0
Enter GPA: 3.8
--- Student 2 ---
Enter name: Bob Jones
Enter age: 24
Enter Program (0 for Undergraduate, 1 for Postgraduate): 1
Enter research hours: 120
===== Student Records =====
Name: Alice Smith
Age: 20
Program: Undergraduate
GPA: 3.80
Name: Bob Jones
Age: 24
Program: Postgraduate
Research Hours: 120