Skip to content
C project #10Intermediate

Library Management System

Catalogue books, issue and return them with due dates and late fines, find overdue books, and keep everything in a text file.

Estimated time
~6 hours to build
Files
7 files · 533 lines
Technologies
C11, <ctype.h>, Text file I/O

Introduction

A library system combines a catalogue with state that changes over time: books move between available and issued, due dates pass and fines accrue. This version simulates the calendar with a day counter so behaviour is deterministic and testable.

Problem statement

Maintain a catalogue of books with auto-generated IDs. Allow searching by title or author (case-insensitive), issuing a book to a member, returning it with a fine of Rs 2 per day after a 14-day loan, listing overdue books, and saving the catalogue in a text file that can be inspected by hand.

Objectives

  • Model state transitions (available ↔ issued) with validation
  • Implement case-insensitive substring search
  • Write and parse a delimited text format
  • Separate business rules from the user interface

Features

  • Auto-incrementing book IDs
  • Issue/return with clear error codes
  • Late fines computed from loan length
  • Overdue report
  • Pipe-separated text storage so titles may contain commas

Concepts demonstrated

  • Structs and arrays
  • Return-code error handling
  • Parsing delimited text
  • Simulated time

System architecture

Each module pairs a header (.h, the interface) with a source file (.c, the implementation); main.c contains only the user interface. library.c implements the rules and storage; functions return 1/0/−1 codes that main.c turns into messages. The catalogue is a fixed array inside a static Library (kept off the stack because it is large). Each book is one line: id|title|author|year|issued|member|issue_day.

Folder structure

TextProject tree
library-management/
├── README.md
├── Makefile
├── src/library.c
├── src/library.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/library.h
Book and Library types, loan constants and the API.
src/library.c
Add/find/issue/return/search rules, case-insensitive matching and the text-file format.
src/main.c
Menu, the current-day counter and report printing.
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
Starting a new catalogue.
Today's day number:
[Day 1] 1.Add 2.List 3.Search 4.Issue 5.Return 6.Overdue 7.Next day 0.Exit
Choice:
Title:
Author:
Year:
Added book 1001.
[Day 1] 1.Add 2.List 3.Search 4.Issue 5.Return 6.Overdue 7.Next day 0.Exit
Choice:
Title:
Author:
Year:
Added book 1002.
[Day 1] 1.Add 2.List 3.Search 4.Issue 5.Return 6.Overdue 7.Next day 0.Exit
Choice:
Title:
Author:
Year:
Added book 1003.
[Day 1] 1.Add 2.List 3.Search 4.Issue 5.Return 6.Overdue 7.Next day 0.Exit
Choice:
Book ID:
Member name:
Book 1001 issued to Priya. Due on day 15.
[Day 1] 1.Add 2.List 3.Search 4.Issue 5.Return 6.Overdue 7.Next day 0.Exit
Choice:
Book ID:
Member name:
Book 1001 is already issued.
[Day 1] 1.Add 2.List 3.Search 4.Issue 5.Return 6.Overdue 7.Next day 0.Exit
Choice:
Book ID:
Member name:
Book 1003 issued to Rahul. Due on day 15.
[Day 1] 1.Add 2.List 3.Search 4.Issue 5.Return 6.Overdue 7.Next day 0.Exit
Choice:
Days to advance:
Today is day 11.
[Day 11] 1.Add 2.List 3.Search 4.Issue 5.Return 6.Overdue 7.Next day 0.Exit
Choice:
Book ID:
Book 1003 returned on time.
[Day 11] 1.Add 2.List 3.Search 4.Issue 5.Return 6.Overdue 7.Next day 0.Exit
Choice:
Days to advance:
Today is day 21.
[Day 21] 1.Add 2.List 3.Search 4.Issue 5.Return 6.Overdue 7.Next day 0.Exit
Choice:
ID Title Author Year Status
1001 The C Programming Language Kernighan and Ritc 1988 issued to Priya, due day 15 (OVERDUE)
[Day 21] 1.Add 2.List 3.Search 4.Issue 5.Return 6.Overdue 7.Next day 0.Exit
Choice:
Book ID:
Book 1001 returned late. Fine: Rs 12.
[Day 21] 1.Add 2.List 3.Search 4.Issue 5.Return 6.Overdue 7.Next day 0.Exit
Choice:
Search title or author:
ID Title Author Year Status
1002 Clean Code Robert Martin 2008 available
[Day 21] 1.Add 2.List 3.Search 4.Issue 5.Return 6.Overdue 7.Next day 0.Exit
Choice:
ID Title Author Year Status
1001 The C Programming Language Kernighan and Ritc 1988 available
1002 Clean Code Robert Martin 2008 available
1003 Introduction to Algorithms Cormen et al. 2009 available
[Day 21] 1.Add 2.List 3.Search 4.Issue 5.Return 6.Overdue 7.Next day 0.Exit
Choice:
Catalogue saved to library.txt.
Input
1
1
The C Programming Language
Kernighan and Ritchie
1988
1
Clean Code
Robert Martin
2008
1
Introduction to Algorithms
Cormen et al.
2009
4
1001
Priya
4
1001
Rahul
4
1003
Rahul
7
10
5
1003
7
10
6
5
1001
3
clean
2
0

Testing

  • Diff the sample run against tests/expected-output.txt (includes an already-issued error, an on-time return and a late return with a Rs 12 fine).
  • Open library.txt in a text editor after a run and check its format.
  • Restart the program and confirm issued books are still issued.

Common errors

Titles cut off at a comma after reload

Use the | separator (as this project does); commas are common in titles.

Fine is negative

Only charge for days beyond the loan period: late > 0 ? late * FINE_PER_DAY : 0.

Possible improvements

  • Add members with borrowing limits
  • Reserve books that are currently issued
  • Use real dates with <time.h>

Additional challenges

  1. 1Limit each member to 3 books at a time.
  2. 2Add a report of the five most borrowed books (track an issue count).
  3. 3Store the catalogue in a binary file with random-access updates.

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