Skip to content
C project #11Intermediate

Inventory Management System

Track products by SKU, receive and dispatch stock, flag reorder levels and value the inventory — stored as a spreadsheet-ready CSV.

Estimated time
~5 hours to build
Files
7 files · 459 lines
Technologies
C11, CSV, <stdlib.h> qsort

Introduction

Shops and warehouses need to know what they have, what is running low and what their stock is worth. This project implements those core inventory operations with a clean module and a CSV file that opens in Excel or LibreOffice.

Problem statement

Store products (SKU, name, quantity, reorder level, unit price). Support adding products with validation, receiving and dispatching stock without allowing negative quantities, warning when stock falls to the reorder level, listing products to reorder with a suggested quantity, and a valuation report sorted by stock value.

Objectives

  • Build a reusable inventory module on a dynamic array
  • Enforce invariants (no negative stock, unique SKUs)
  • Read and write CSV with fprintf/sscanf scansets
  • Sort by a computed key

Features

  • Unique SKUs; names validated for CSV safety
  • Stock in/out with insufficient-stock protection
  • Reorder warnings and a suggested order quantity
  • Valuation report sorted by value
  • CSV persistence

Concepts demonstrated

  • Data validation
  • Scansets (%[^,])
  • Computed sort keys
  • Modules

System architecture

Each module pairs a header (.h, the interface) with a source file (.c, the implementation); main.c contains only the user interface. Inventory is a growable array of Product. inv_adjust applies a signed delta and refuses to go below zero, returning −1 so the UI can show the available quantity. CSV is loaded with sscanf(line, "%11[^,],%39[^,],%d,%d,%lf", ...).

Folder structure

TextProject tree
inventory-management/
├── README.md
├── Makefile
├── src/inventory.c
├── src/inventory.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/inventory.h
Product and Inventory types and the API.
src/inventory.c
Add/find/adjust, valuation and sorting, CSV save/load.
src/main.c
Menu, input validation and report formatting.
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
No inventory file found; starting empty.
1.Add product 2.Receive stock 3.Dispatch stock 4.Report 5.Reorder list 6.Valuation 0.Save & exit
Choice:
SKU:
Name:
Opening quantity:
Reorder level:
Unit price:
Product PEN-01 added.
1.Add product 2.Receive stock 3.Dispatch stock 4.Report 5.Reorder list 6.Valuation 0.Save & exit
Choice:
SKU:
Name:
Opening quantity:
Reorder level:
Unit price:
Product NB-A4 added.
1.Add product 2.Receive stock 3.Dispatch stock 4.Report 5.Reorder list 6.Valuation 0.Save & exit
Choice:
SKU:
Name:
Opening quantity:
Reorder level:
Unit price:
Product STP-10 added.
1.Add product 2.Receive stock 3.Dispatch stock 4.Report 5.Reorder list 6.Valuation 0.Save & exit
Choice:
SKU:
Quantity:
PEN-01 now has 50 unit(s).
Warning: PEN-01 is at or below its reorder level (100).
1.Add product 2.Receive stock 3.Dispatch stock 4.Report 5.Reorder list 6.Valuation 0.Save & exit
Choice:
SKU:
Quantity:
Only 15 unit(s) of STP-10 in stock.
1.Add product 2.Receive stock 3.Dispatch stock 4.Report 5.Reorder list 6.Valuation 0.Save & exit
Choice:
SKU:
Quantity:
STP-10 now has 20 unit(s).
1.Add product 2.Receive stock 3.Dispatch stock 4.Report 5.Reorder list 6.Valuation 0.Save & exit
Choice:
SKU Product Qty Reorder Price Value
PEN-01 Ball pen (blue) 50 100 10.00 500.00 <- reorder
NB-A4 A4 notebook 120 40 55.00 6600.00
STP-10 Stapler 20 10 180.00 3600.00
Total stock value: Rs 10700.00
1.Add product 2.Receive stock 3.Dispatch stock 4.Report 5.Reorder list 6.Valuation 0.Save & exit
Choice:
PEN-01 Ball pen (blue) qty 50 (reorder at 100) - suggest ordering 150
1.Add product 2.Receive stock 3.Dispatch stock 4.Report 5.Reorder list 6.Valuation 0.Save & exit
Choice:
SKU Product Qty Reorder Price Value
NB-A4 A4 notebook 120 40 55.00 6600.00
STP-10 Stapler 20 10 180.00 3600.00
PEN-01 Ball pen (blue) 50 100 10.00 500.00 <- reorder
Total stock value: Rs 10700.00
1.Add product 2.Receive stock 3.Dispatch stock 4.Report 5.Reorder list 6.Valuation 0.Save & exit
Choice:
Saved to inventory.csv.
Input
1
PEN-01
Ball pen (blue)
500
100
10
1
NB-A4
A4 notebook
120
40
55
1
STP-10
Stapler
15
10
180
3
PEN-01
450
3
STP-10
20
2
STP-10
5
4
5
6
0

Testing

  • Diff with tests/expected-output.txt (includes a reorder warning and a blocked dispatch).
  • Open inventory.csv in a spreadsheet after a run.
  • Enter a negative price or a comma in a name and confirm rejection.

Common errors

Products missing after reload

The CSV header line must be skipped before parsing rows; also check names do not contain commas.

Valuation total slightly off

Prices are doubles; for exact accounting store prices in paise as integers (see the Banking project).

Possible improvements

  • Record every stock movement with a date (an audit trail)
  • Support multiple warehouses
  • Import a supplier price list

Additional challenges

  1. 1Add a search by name fragment.
  2. 2Generate a purchase order file for all items below their reorder level.
  3. 3Store prices as integer paise and print them as rupees.

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