This project has been created as part of the 42 curriculum by gastesan.
- π Description
- π Instructions
- π§ Technical Choices
- π§© Algorithm explanation
- π Resources
- π€ AI usage notice
ft_printf is a reimplementation of the standard libc printf() function as a static library.
The goal of this project is to reproduce the behavior of printf() while respecting
strict constraints imposed by the 42 Norm.
It focuses on variadic functions, format string parsing, buffered output,
and precise formatting rules.
All mandatory conversions (c s p d i u x X %) are implemented as well as bonus flags
(- 0 <space> # +), precision (.) and width fields.
.
βββ README.md
βββ libftprintf.h # Public header
βββ includes/ # Private headers
βββ srcs
βΒ Β βββ ft_printf.c
βΒ Β βββ append.c
βΒ Β βββ rules_parse.c
βΒ Β βββ rules_apply.c
βββ libft/ # My libft static library
This project builds a static library intended to be linked with your programs.
Example compilation:
make bonus
cc <my_main.c> libftprintf.a -Iincludes
libftis automatically built and linked bymakerules (used for string utilities and memory helpers).
ft_printf("Value: %08x | ptr: %p | str: %.5s\n", nb, ptr, str);Both the output and return value matches standard printf():
- the return value is the number of characters written (
-1on error) - the output is written directly to
stdoutusingwrite().
Instead of writing directly to stdout on every conversion, output is accumulated
inside a dynamic buffer.
This significantly reduces syscall overhead and allows complex formatting to be applied
before emission.
Buffers grow exponentially to minimize reallocations and ensure O(1) amortized append complexity.
Formatting is split into two clear phases:
- Raw value append (conversion-specific)
- Rule application (precision, sign, width, padding, prefixes)
This separation improves readability, extensibility, and correctness.
Parsing normalizes conflicting flags early:
-disables0- precision disables
0 +overrides space- invalid flags for a conversion are ignored
- ...
This avoids the original printf() undefined behaviors and ensures deterministic formatting.
- Temporary buffers are
free'd immediately - No global state
- All allocations are checked
- No leaks under normal execution
The ft_printf prototype is annotated with __attribute__((format(printf, 1, 2))).
This enables the compiler and IDEs to perform the same format string
checks as for the standard printf(), including:
- format / argument type mismatches
- missing or extra arguments
This provides early error detection at compile time and improves developer experience without impacting runtime behavior.
βοΈ Same safety guarantees as printf, zero runtime cost.
- Initialize the main output buffer
t_buff. - Iterate through the format string.
- Copy literal text until
%to the main buffer. - Parse formatting rules into a
t_rulesstructure. - Dispatch conversion to a dedicated append function.
- Convert raw value to a temporary buffer.
- Apply formatting rules to the temporary buffer.
- Append the result to the main buffer.
- Write the final buffer to
stdoutin one call.
Stores all parsed flags:
- padding
- width
- precision
- sign rules
- conversion type
Dynamic byte buffer with:
- capacity
- length
- raw data pointer
Supports prepend, insert, and append operations with automatic exponential growth.
- Time: O(n) per formatted output
- Space: proportional to output size
- Each character is copied a bounded number of times
man 3 printfman 3 stdarg- GNU libc printf documentation
- 42 subject
- Norm.v4
RTFM π€
No code was generated by AI.
AI was used only to:
- reason about edge cases and formatting behavior,
- clarify tester/compiler logs,
- refine explanations and documentation.
All technical decisions and implementations are original.