Skip to content

Hullaah/printf

Repository files navigation

🔧 printf Clone in C — From Scratch Using Only write(2)

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.


📌 Motivation

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

✨ Key Features

✅ Format Specifiers Supported:

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

🧾 Supported Flags:

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)

📏 Width & Precision:

  • Field width supported (e.g. %10d)
  • Precision supported for strings and numbers (e.g. %.5s, %.4d)
  • Combined width & precision supported (e.g. %08.4d)

🧱 Length Modifiers:

Modifier Meaning
hh char or unsigned char
h short or unsigned short
l long or unsigned long
ll long long or unsigned long long

⚙️ Core Architecture

  • ✅ 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

🛠️ Building the Project

🔧 Requirements

  • GCC or Clang
  • make
  • Unix-like OS (Linux, macOS, WSL, etc.)

🧪 Build Instructions

  1. Clone the repository:
git clone https://github.com/Hullaah/printf.git
cd printf
  1. Build the project
make
  1. Optionally, generate compile_commands.json for code navigation:
bear -- make
  1. Run the Demo
./printf

🧠 What I Learned

  • 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

🏁 Future Work

  • 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

👨‍💻 Author

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.

📧 Contact me

🌐 GitHub: @Hullaah

About

An Implementation of the C standard library printf

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published