Skip to content
C project #17Intermediate

Hospital Management System

Register patients, book appointments with clash detection for doctors and patients, view daily schedules and patient history.

Estimated time
~7 hours to build
Files
7 files · 463 lines
Technologies
C11, Binary file I/O

Introduction

Scheduling systems must stop two things happening at once: a doctor seeing two patients in one slot, or a patient booked with two doctors. This project models patients, doctors and appointments and enforces those constraints.

Problem statement

Register patients, list doctors (three are created on first run), book appointments for a day and hourly slot, reject clashes, cancel appointments (keeping them in history), print a doctor's schedule for a day and a patient's full history. Save all data between runs.

Objectives

  • Model related entities with IDs
  • Enforce scheduling constraints
  • Return descriptive error codes
  • Persist a whole database struct

Features

  • Patient registration
  • Doctor list with specialties
  • Booking with doctor and patient clash checks
  • Cancellation with history kept
  • Daily doctor schedule and patient history

Concepts demonstrated

  • Entity relationships
  • Constraint checking
  • Error codes
  • Lookup tables

System architecture

Each module pairs a header (.h, the interface) with a source file (.c, the implementation); main.c contains only the user interface. All data sits in one Hospital struct (arrays of patients, doctors and appointments). book returns an appointment ID or a negative error code that indexes an error-message table in main.c. Saving writes the whole struct with one fwrite.

Folder structure

TextProject tree
hospital-management/
├── README.md
├── Makefile
├── src/hospital.c
├── src/hospital.h
├── src/main.c
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CC = gcc
CFLAGS = -std=c11 -Wall -Wextra -O2
SRC := $(wildcard src/*.c)
HDR := $(wildcard src/*.h)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CC) $(CFLAGS) -Isrc $(SRC) -o app -lm
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/hospital.h
Entity types, limits and the API.
src/hospital.c
Registration, lookup, booking rules, cancellation, slot labels and persistence.
src/main.c
Menu, schedule and history reports, and default doctors.
Makefile
Build automation: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
gcc -std=c11 -Wall -Wextra -Isrc src/*.c -o app -lm
# or simply: make

Run

Terminal
./app # Linux / macOS
.\app.exe # Windows

Test against the expected output

Terminal
./app < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# Get-Content tests\sample-input.txt | .\app.exe > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
New hospital database created with 3 doctors.
1.Register patient 2.Doctors 3.Book 4.Cancel 5.Doctor schedule 6.Patient history 0.Save & exit
Choice:
Name (no spaces), age, gender (M/F/O), phone:
Registered patient 1.
1.Register patient 2.Doctors 3.Book 4.Cancel 5.Doctor schedule 6.Patient history 0.Save & exit
Choice:
Name (no spaces), age, gender (M/F/O), phone:
Registered patient 2.
1.Register patient 2.Doctors 3.Book 4.Cancel 5.Doctor schedule 6.Patient history 0.Save & exit
Choice:
ID Doctor Specialty
101 Dr. A. Menon General Medicine
102 Dr. S. Iyer Cardiology
103 Dr. R. Khan Orthopaedics
1.Register patient 2.Doctors 3.Book 4.Cancel 5.Doctor schedule 6.Patient history 0.Save & exit
Choice:
Patient ID, doctor ID, day, slot (0=09:00 ... 7=16:00):
Appointment #1 booked: day 1 at 09:00 with Dr. A. Menon.
1.Register patient 2.Doctors 3.Book 4.Cancel 5.Doctor schedule 6.Patient history 0.Save & exit
Choice:
Patient ID, doctor ID, day, slot (0=09:00 ... 7=16:00):
Cannot book: doctor already booked at that time.
1.Register patient 2.Doctors 3.Book 4.Cancel 5.Doctor schedule 6.Patient history 0.Save & exit
Choice:
Patient ID, doctor ID, day, slot (0=09:00 ... 7=16:00):
Appointment #2 booked: day 1 at 09:00 with Dr. S. Iyer.
1.Register patient 2.Doctors 3.Book 4.Cancel 5.Doctor schedule 6.Patient history 0.Save & exit
Choice:
Patient ID, doctor ID, day, slot (0=09:00 ... 7=16:00):
Cannot book: patient already has an appointment then.
1.Register patient 2.Doctors 3.Book 4.Cancel 5.Doctor schedule 6.Patient history 0.Save & exit
Choice:
Patient ID, doctor ID, day, slot (0=09:00 ... 7=16:00):
Appointment #3 booked: day 1 at 11:00 with Dr. A. Menon.
1.Register patient 2.Doctors 3.Book 4.Cancel 5.Doctor schedule 6.Patient history 0.Save & exit
Choice:
Doctor ID and day:
Schedule for Dr. A. Menon on day 1:
09:00 Sita
10:00 free
11:00 Ram
12:00 free
13:00 free
14:00 free
15:00 free
16:00 free
1.Register patient 2.Doctors 3.Book 4.Cancel 5.Doctor schedule 6.Patient history 0.Save & exit
Choice:
Appointment number:
Appointment #1 cancelled.
1.Register patient 2.Doctors 3.Book 4.Cancel 5.Doctor schedule 6.Patient history 0.Save & exit
Choice:
Patient ID:
Patient 1: Sita, 34, F, phone 9876500001
#1 day 1 09:00 with Dr. A. Menon (cancelled)
1.Register patient 2.Doctors 3.Book 4.Cancel 5.Doctor schedule 6.Patient history 0.Save & exit
Choice:
Data saved.
Input
1
Sita 34 F 9876500001
1
Ram 61 M 9876500002
2
3
1 101 1 0
3
2 101 1 0
3
2 102 1 0
3
1 103 1 0
3
2 101 1 2
5
101 1
4
1
6
1
0

Testing

  • Diff with tests/expected-output.txt (one doctor clash and one patient clash are rejected).
  • Cancel an appointment and book the same slot again.
  • Restart and confirm the data is kept.

Common errors

Old hospital.dat makes the program misbehave

The file is a raw struct dump; delete it after changing any struct definition.

Two patients with the same name

Names are not unique; always use the patient ID.

Possible improvements

  • Billing and prescriptions
  • Search patients by name
  • Weekly calendar view

Additional challenges

  1. 1Find the next free slot for a doctor automatically.
  2. 2Add a waiting list when a doctor's day is full.
  3. 3Move storage to a text format readable by other programs.

Built and tested it yourself? Mark it to update your dashboard.