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
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
Complete source code
Makefile
What each file does
- src/inventory.h
ProductandInventorytypes 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:
makebuilds./app,make testruns the sample session and compares it with the expected output,make cleanremoves build files.
How to run it
Build
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
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
- 1Add a search by name fragment.
- 2Generate a purchase order file for all items below their reorder level.
- 3Store prices as integer paise and print them as rupees.
Built and tested it yourself? Mark it to update your dashboard.