
2023Systems Programming
Custom Printf — C Implementation
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.
Le Défi
- Parsing format strings with arbitrary combinations of flags, width, precision, and specifiers
- Handling variadic arguments safely across all supported conversion types
- Implementing non-standard specifiers like %b (binary), %r (reverse), and %R (ROT13)
La Solution
- Designed a modular dispatch table mapping each specifier character to its handler function.
- Used va_list, va_start, va_arg, and va_end idioms correctly across all conversion branches.
- Implemented separate helper modules (helpers.c, conversion.c, modifiers.c) to keep the codebase maintainable and Betty-compliant.
Fonctionnalités Clés
- 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
Processus
Phase 01
Defined function prototypes and data structures in main.h
Phase 02
Built the core format string parser loop in _printf
Phase 03
Implemented each conversion specifier as a separate reusable function
Phase 04
Added length modifier, field width, precision, and flag character handling in modifiers.c
Phase 05
Compiled and tested with strict gcc flags on Ubuntu 20.04 LTS