This project is a full-featured reimplementation of the standard C printf
function — built entirely from scratch using only the low-level write
system call.
It includes its own buffering system, format specifier parser, and modular handlers for each format type — without depending on the standard library's printf
, sprintf
, or related routines.
The standard C library's printf
is deceptively complex, involving variadic arguments, formatting rules, padding logic, and internal buffering.
This project was created to:
- Deepen understanding of low-level I/O, format parsing, and variadic functions
- Gain practical experience building reusable, modular code under constraints
Specifier | Meaning |
---|---|
%c |
Character |
%s |
String |
%d /%i |
Signed decimal integer |
%u |
Unsigned decimal integer |
%x |
Lowercase hexadecimal |
%X |
Uppercase hexadecimal |
%o |
Octal |
%% |
Literal % character |
Flag | Meaning |
---|---|
- |
Left-align within the field width |
+ |
Always print the sign (+ or - ) |
(space) | Print space if no sign is used |
0 |
Pad with zeros instead of spaces |
# |
Use alternate form (0x , 0X , 0 etc) |
- Field width supported (e.g.
%10d
) - Precision supported for strings and numbers (e.g.
%.5s
,%.4d
) - Combined width & precision supported (e.g.
%08.4d
)
Modifier | Meaning |
---|---|
hh |
char or unsigned char |
h |
short or unsigned short |
l |
long or unsigned long |
ll |
long long or unsigned long long |
- ✅ Manual parsing of format strings
- ✅ Variadic argument handling with
stdarg.h
- ✅ Internal output buffer with flushing control
- ✅ Edge-case handling:
LLONG_MIN
signed overflow- Mixed flags (
+
,0
,-
) - Zero precision with zero value (
%.0d
) - Buffer overflow protection
- GCC or Clang
make
- Unix-like OS (Linux, macOS, WSL, etc.)
- Clone the repository:
git clone https://github.com/Hullaah/printf.git
cd printf
- Build the project
make
- Optionally, generate compile_commands.json for code navigation:
bear -- make
- Run the Demo
./printf
- How printf actually works under the hood
- Designing and implementing format parsers
- Manual buffer management and flushing logic
- Respecting field width, precision, flags, and length modifiers
- Debugging tricky issues like signed integer overflow (LLONG_MIN)
- Writing clean, maintainable low-level C code
- Add support for floating-point formats (%f, %e, %g)
- Extend support for wide characters (%ls, %lc)
- Add unit tests for all specifiers
- Benchmark performance against libc printf
- Port to kernel-space with printk-like behavior
Umar Adelowo
Intermediate systems programmer focused on OS, networking, and low-level development. Aiming to contribute to the Linux Kernel and become a security and systems expert.
🌐 GitHub: @Hullaah