
Custom Printf — C Implementation
01. Overview
A custom implementation of the C standard library printf function, built from scratch in C. Supports a comprehensive set of conversion specifiers (%c, %s, %d, %i, %u, %o, %x, %X, %p, %b, %r, %R, %S) and follows the Betty style guidelines, compiled on Ubuntu 20.04 with gcc.
The Objective
To deeply understand the internals of the C standard library by re-implementing printf from scratch, handling format string parsing, type conversions, and variadic argument processing.
The Outcome
A modular, production-quality C implementation of printf that passes all standard tests, is compiled with strict gcc flags (-Wall -Werror -Wextra -pedantic), and covers 13+ conversion specifiers.
02. Stack Architecture
03. Key Features
Supports %c, %s, %%, %d, %i, %u, %o, %x, %X, %p, %S, %b, %r, %R
Modular source structure (helpers.c, conversion.c, modifiers.c)
Handles length modifiers, field width, precision, and flag characters
Compiled with -Wall -Werror -Wextra -pedantic -std=gnu89
Follows Betty C style guidelines
04. Engineering Pipeline
Defined function prototypes and data structures in main.h
Built the core format string parser loop in _printf
Implemented each conversion specifier as a separate reusable function
Added length modifier, field width, precision, and flag character handling in modifiers.c
Compiled and tested with strict gcc flags on Ubuntu 20.04 LTS
05. Challenges & Execution
The Constraint
Parsing format strings with arbitrary combinations of flags, width, precision, and specifiers
The Execution
Designed a modular dispatch table mapping each specifier character to its handler function.
The Constraint
Handling variadic arguments safely across all supported conversion types
The Execution
Used va_list, va_start, va_arg, and va_end idioms correctly across all conversion branches.
The Constraint
Implementing non-standard specifiers like %b (binary), %r (reverse), and %R (ROT13)
The Execution
Implemented separate helper modules (helpers.c, conversion.c, modifiers.c) to keep the codebase maintainable and Betty-compliant.