How rchallie/ft_printf works
1. Repository purpose
- This is a C implementation of printf-style formatted output for the 42 cursus.
- The deliverable is a small formatting library, not an application: the main output path is a function named ft_printf, intended to behave like standard printf for supported conversions.
2. Top-level build boundary
- The project is built with a Makefile into a static archive using the local libft library.
- The repository is split into:
- includes/ for the public header,
- sources/ for the formatter implementation,
- libft/ for reusable utility functions and the compiled libft.a.
3. Public interface
- includes/ft_printf.h is the main contract exposed to consumers.
- It likely declares the ft_printf entry point plus the internal helper routines used across the formatter pipeline.
- This header is the boundary between the external caller and the formatting engine.
4. Core control flow
- The formatter is organized as a parse-and-dispatch pipeline:
- scan the format string,
- detect % sequences,
- interpret flags/width/precision/specifier,
- route to a type-specific handler,
- emit formatted characters to the output sink.
- The file names suggest sources/ft_treatment.c and sources/ft_treat_flags.c act as the central orchestration layer.
5. Format parsing subsystem
- ft_treat_flags.c, ft_treat_width.c, and likely ft_treatment.c handle the syntax of format directives.
- This subsystem resolves standard printf modifiers such as flags, field width, and precision before value conversion happens.
- The architecture separates parsing from rendering, which makes each specifier handler simpler.
6. Specifier-specific rendering
- Dedicated modules handle each supported conversion:
- ft_treat_char.c for characters,
- ft_treat_string.c for strings,
- ft_treat_int.c for signed integers,
- ft_treat_uint.c for unsigned integers,
- ft_treat_hexa.c for hexadecimal output,
- ft_treat_pointer.c for pointer formatting,
- ft_treat_percent.c for literal %.
- This indicates a dispatcher architecture where one parsed directive fans out to the appropriate formatter.
7. Numeric conversion helpers
- ft_u_itoa.c, ft_ull_base.c, and ft_str_tolower.c support integer-to-string conversion and base transformation.
- These helpers isolate number formatting concerns from the higher-level printf logic.
- ft_str_tolower.c is especially relevant for hexadecimal lowercasing.
8. Padding, alignment, and precision
- ft_putstrprec.c and ft_treat_width.c suggest dedicated logic for truncation, padding, and alignment.
- In a printf implementation, these concerns are cross-cutting across all specifiers, so they are separated into reusable formatting helpers rather than duplicated in each type handler.
9. Output sink
- ft_putchar.c is the low-level emission primitive used by the rest of the formatter.
- The repository also includes libft/ft_putchar_fd.c, ft_putstr_fd.c, and related routines, which indicates the output model is based on writing characters/strings to a file descriptor.
- The actual user-visible behavior is likely accumulated through repeated writes and a returned character count.
10. Dependency on libft
- The included libft is a large internal utility layer providing string, memory, conversion, and list functions.
- ft_printf relies on this library for basic operations such as string length, duplication, substring handling, allocation, and numeric/string helpers.
- Architecturally, libft is a shared foundation, while sources/ contains printf-specific logic.
11. Runtime model
- The code is pure C, synchronous, and single-process: there are no threads, services, network calls, or persistence layers.
- All state is transient during a call to ft_printf, making the system easy to diagram as a linear formatting pipeline from input format string to output descriptor.
12. What the diagram should show
- A useful architecture diagram would have:
- the caller invoking ft_printf,
- the parser/dispatcher in the middle,
- separate handler boxes for each conversion type,
- shared helper boxes for width/precision/number conversion,
- libft as a supporting utility dependency,
- and the final output target as a file descriptor/stream.
13. Project scope note
- This repo appears to target the standard 42 printf subset, so the architecture is intentionally modular around supported specifiers rather than a full libc replacement.
- The absence of broader application code means the important boundary is the formatting engine itself, not an external system integration.
Main entry points: ft_treatment.c treatment · ft_putchar.c putchar · libft.a libft · ft_treat_hexa.c hexa
Components 19
Build
Static library build and packaging boundary
- Makefile · Builds the static archive and links libft
Makefile
Public API
Consumer-facing contract
- ft_printf.h · Public contract for ft_printf and helper declarations
includes/ft_printf.h
Formatter
Printf parsing, dispatch, and rendering
- ft_printf · Top-level formatted output function
ft_printf.c - treatment · Scans the format string and routes each directive
sources/ft_treatment.c - flags · Interprets printf flags and modifier state
sources/ft_treat_flags.c - width · Resolves field width and alignment behavior
sources/ft_treat_width.c - char · Renders character conversions
sources/ft_treat_char.c - string · Renders string conversions with precision support
sources/ft_treat_string.c - int · Renders signed integer conversions
sources/ft_treat_int.c - uint · Renders unsigned integer conversions
sources/ft_treat_uint.c - hexa · Renders hexadecimal conversions
sources/ft_treat_hexa.c - pointer · Renders pointer conversions
sources/ft_treat_pointer.c - percent · Renders literal percent output
sources/ft_treat_percent.c - putchar · Low-level character emission primitive
sources/ft_putchar.c
Support
Shared utilities from libft and local helpers
- putstrprec · String truncation and precision-aware output
sources/ft_putstrprec.c - u_itoa · Unsigned integer to string conversion
sources/ft_u_itoa.c - ull_base · Base conversion for unsigned long long values
sources/ft_ull_base.c - tolower · Lowercases hexadecimal digits and related text
sources/ft_str_tolower.c - libft · Shared string, memory, conversion, and fd helpers
libft/libft.a
Connections 30
- Makefile → ft_printf: builds · Compiles the printf library objects and archive
- Makefile → libft: links · Consumes the bundled libft archive
- ft_printf.h → ft_printf: declares · Exposes the public ft_printf interface
- ft_printf → treatment: dispatches · Hands the format string to the formatting pipeline
- treatment → flags: parses · Extracts formatting flags from each directive
- treatment → width: parses · Resolves width and padding state
- treatment → char: routes · Handles %c directives
- treatment → string: routes · Handles %s directives
- treatment → int: routes · Handles signed integer directives
- treatment → uint: routes · Handles unsigned integer directives
- treatment → hexa: routes · Handles hexadecimal directives
- treatment → pointer: routes · Handles pointer directives
- treatment → percent: routes · Handles literal percent output
- string → putstrprec: uses · Applies precision-aware string output
- int → u_itoa: uses · Converts signed values through unsigned formatting helpers
- uint → u_itoa: uses · Formats unsigned values as decimal text
- hexa → ull_base: uses · Converts values in hexadecimal base
- hexa → tolower: uses · Normalizes lowercase hexadecimal output
- pointer → ull_base: uses · Formats addresses through base conversion
- char → putchar: emits · Writes characters to the output sink
- string → putchar: emits · Writes strings character by character
- int → putchar: emits · Writes formatted numeric output
- uint → putchar: emits · Writes formatted numeric output
- hexa → putchar: emits · Writes formatted hexadecimal output
- pointer → putchar: emits · Writes pointer output with prefix
- percent → putchar: emits · Writes the literal percent character
- treatment → libft: depends on · Uses shared string and memory primitives during parsing
- putstrprec → libft: depends on · Relies on common string utilities
- u_itoa → libft: depends on · Relies on allocation and string helpers
- ull_base → libft: depends on · Relies on shared utility functions