# rchallie/ft_printf architecture

> Architecture diagram of the GitHub repository rchallie/ft_printf, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-04). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rchallie/ft_printf
- Repository: https://github.com/rchallie/ft_printf

## Overview

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: [`sources/ft_treatment.c`](https://github.com/rchallie/ft_printf/blob/master/sources/ft_treatment.c) (treatment), [`sources/ft_putchar.c`](https://github.com/rchallie/ft_printf/blob/master/sources/ft_putchar.c) (putchar), [`libft/libft.a`](https://github.com/rchallie/ft_printf/blob/master/libft/libft.a) (libft), [`sources/ft_treat_hexa.c`](https://github.com/rchallie/ft_printf/blob/master/sources/ft_treat_hexa.c) (hexa)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_build["Build"]
  node_makefile["Makefile<br/>build"]
end

subgraph group_public["Public API"]
  node_header["ft_printf.h<br/>api header<br/>[ft_printf.h]"]
end

subgraph group_core["Formatter"]
  node_entry["ft_printf<br/>entrypoint<br/>[ft_printf.c]"]
  node_treatment["treatment<br/>dispatcher<br/>[ft_treatment.c]"]
  node_flags["flags<br/>parser<br/>[ft_treat_flags.c]"]
  node_width["width<br/>parser<br/>[ft_treat_width.c]"]
  node_char_t["char<br/>specifier<br/>[ft_treat_char.c]"]
  node_string_t["string<br/>specifier<br/>[ft_treat_string.c]"]
  node_int_t["int<br/>specifier<br/>[ft_treat_int.c]"]
  node_uint_t["uint<br/>specifier<br/>[ft_treat_uint.c]"]
  node_hexa_t["hexa<br/>specifier<br/>[ft_treat_hexa.c]"]
  node_ptr_t["pointer<br/>specifier<br/>[ft_treat_pointer.c]"]
  node_percent_t["percent<br/>specifier<br/>[ft_treat_percent.c]"]
  node_putchar(("putchar<br/>sink<br/>[ft_putchar.c]"))
end

subgraph group_support["Support"]
  node_putstrprec["putstrprec<br/>format helper<br/>[ft_putstrprec.c]"]
  node_u_itoa["u_itoa<br/>number helper<br/>[ft_u_itoa.c]"]
  node_ull_base["ull_base<br/>number helper<br/>[ft_ull_base.c]"]
  node_tolower["tolower<br/>text helper<br/>[ft_str_tolower.c]"]
  node_libft[("libft<br/>utility library<br/>[libft.a]")]
end

node_makefile -->|"builds"| node_entry
node_makefile -->|"links"| node_libft
node_header -->|"declares"| node_entry
node_entry -->|"dispatches"| node_treatment
node_treatment -->|"parses"| node_flags
node_treatment -->|"parses"| node_width
node_treatment -->|"routes"| node_char_t
node_treatment -->|"routes"| node_string_t
node_treatment -->|"routes"| node_int_t
node_treatment -->|"routes"| node_uint_t
node_treatment -->|"routes"| node_hexa_t
node_treatment -->|"routes"| node_ptr_t
node_treatment -->|"routes"| node_percent_t
node_string_t -->|"uses"| node_putstrprec
node_int_t -->|"uses"| node_u_itoa
node_uint_t -->|"uses"| node_u_itoa
node_hexa_t -->|"uses"| node_ull_base
node_hexa_t -->|"uses"| node_tolower
node_ptr_t -->|"uses"| node_ull_base
node_char_t -->|"emits"| node_putchar
node_string_t -->|"emits"| node_putchar
node_int_t -->|"emits"| node_putchar
node_uint_t -->|"emits"| node_putchar
node_hexa_t -->|"emits"| node_putchar
node_ptr_t -->|"emits"| node_putchar
node_percent_t -->|"emits"| node_putchar
node_treatment -.->|"depends on"| node_libft
node_putstrprec -.->|"depends on"| node_libft
node_u_itoa -.->|"depends on"| node_libft
node_ull_base -.->|"depends on"| node_libft

click node_makefile "https://github.com/rchallie/ft_printf/tree/master/Makefile"
click node_header "https://github.com/rchallie/ft_printf/blob/master/includes/ft_printf.h"
click node_entry "https://github.com/rchallie/ft_printf/blob/master/ft_printf.c"
click node_treatment "https://github.com/rchallie/ft_printf/blob/master/sources/ft_treatment.c"
click node_flags "https://github.com/rchallie/ft_printf/blob/master/sources/ft_treat_flags.c"
click node_width "https://github.com/rchallie/ft_printf/blob/master/sources/ft_treat_width.c"
click node_char_t "https://github.com/rchallie/ft_printf/blob/master/sources/ft_treat_char.c"
click node_string_t "https://github.com/rchallie/ft_printf/blob/master/sources/ft_treat_string.c"
click node_int_t "https://github.com/rchallie/ft_printf/blob/master/sources/ft_treat_int.c"
click node_uint_t "https://github.com/rchallie/ft_printf/blob/master/sources/ft_treat_uint.c"
click node_hexa_t "https://github.com/rchallie/ft_printf/blob/master/sources/ft_treat_hexa.c"
click node_ptr_t "https://github.com/rchallie/ft_printf/blob/master/sources/ft_treat_pointer.c"
click node_percent_t "https://github.com/rchallie/ft_printf/blob/master/sources/ft_treat_percent.c"
click node_putchar "https://github.com/rchallie/ft_printf/blob/master/sources/ft_putchar.c"
click node_putstrprec "https://github.com/rchallie/ft_printf/blob/master/sources/ft_putstrprec.c"
click node_u_itoa "https://github.com/rchallie/ft_printf/blob/master/sources/ft_u_itoa.c"
click node_ull_base "https://github.com/rchallie/ft_printf/blob/master/sources/ft_ull_base.c"
click node_tolower "https://github.com/rchallie/ft_printf/blob/master/sources/ft_str_tolower.c"
click node_libft "https://github.com/rchallie/ft_printf/blob/master/libft/libft.a"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_makefile toneBlue
class node_header toneAmber
class node_entry,node_treatment,node_flags,node_width,node_char_t,node_string_t,node_int_t,node_uint_t,node_hexa_t,node_ptr_t,node_percent_t,node_putchar toneMint
class node_putstrprec,node_u_itoa,node_ull_base,node_tolower,node_libft toneRose
```

## Components

### Build

Static library build and packaging boundary

- [Makefile](https://github.com/rchallie/ft_printf/tree/master/Makefile): build · Builds the static archive and links libft · `Makefile`

### Public API

Consumer-facing contract

- [ft_printf.h](https://github.com/rchallie/ft_printf/blob/master/includes/ft_printf.h): api header · Public contract for ft_printf and helper declarations · `includes/ft_printf.h`

### Formatter

Printf parsing, dispatch, and rendering

- [ft_printf](https://github.com/rchallie/ft_printf/blob/master/ft_printf.c): entrypoint · Top-level formatted output function · `ft_printf.c`
- [treatment](https://github.com/rchallie/ft_printf/blob/master/sources/ft_treatment.c): dispatcher · Scans the format string and routes each directive · `sources/ft_treatment.c`
- [flags](https://github.com/rchallie/ft_printf/blob/master/sources/ft_treat_flags.c): parser · Interprets printf flags and modifier state · `sources/ft_treat_flags.c`
- [width](https://github.com/rchallie/ft_printf/blob/master/sources/ft_treat_width.c): parser · Resolves field width and alignment behavior · `sources/ft_treat_width.c`
- [char](https://github.com/rchallie/ft_printf/blob/master/sources/ft_treat_char.c): specifier · Renders character conversions · `sources/ft_treat_char.c`
- [string](https://github.com/rchallie/ft_printf/blob/master/sources/ft_treat_string.c): specifier · Renders string conversions with precision support · `sources/ft_treat_string.c`
- [int](https://github.com/rchallie/ft_printf/blob/master/sources/ft_treat_int.c): specifier · Renders signed integer conversions · `sources/ft_treat_int.c`
- [uint](https://github.com/rchallie/ft_printf/blob/master/sources/ft_treat_uint.c): specifier · Renders unsigned integer conversions · `sources/ft_treat_uint.c`
- [hexa](https://github.com/rchallie/ft_printf/blob/master/sources/ft_treat_hexa.c): specifier · Renders hexadecimal conversions · `sources/ft_treat_hexa.c`
- [pointer](https://github.com/rchallie/ft_printf/blob/master/sources/ft_treat_pointer.c): specifier · Renders pointer conversions · `sources/ft_treat_pointer.c`
- [percent](https://github.com/rchallie/ft_printf/blob/master/sources/ft_treat_percent.c): specifier · Renders literal percent output · `sources/ft_treat_percent.c`
- [putchar](https://github.com/rchallie/ft_printf/blob/master/sources/ft_putchar.c): sink · Low-level character emission primitive · `sources/ft_putchar.c`

### Support

Shared utilities from libft and local helpers

- [putstrprec](https://github.com/rchallie/ft_printf/blob/master/sources/ft_putstrprec.c): format helper · String truncation and precision-aware output · `sources/ft_putstrprec.c`
- [u_itoa](https://github.com/rchallie/ft_printf/blob/master/sources/ft_u_itoa.c): number helper · Unsigned integer to string conversion · `sources/ft_u_itoa.c`
- [ull_base](https://github.com/rchallie/ft_printf/blob/master/sources/ft_ull_base.c): number helper · Base conversion for unsigned long long values · `sources/ft_ull_base.c`
- [tolower](https://github.com/rchallie/ft_printf/blob/master/sources/ft_str_tolower.c): text helper · Lowercases hexadecimal digits and related text · `sources/ft_str_tolower.c`
- [libft](https://github.com/rchallie/ft_printf/blob/master/libft/libft.a): utility library · Shared string, memory, conversion, and fd helpers · `libft/libft.a`

## Connections

- 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
