This project has been created as part of the 42 curriculum by dchernyk.
A recreation of the C standard library printf() function, implemented as a static library libftprintf.a. The main goal of this project is to understand how variadic arguments (va_args) work in C.
The library handles the following format specifiers: %c, %s, %p, %d, %i, %u, %x, %X, %%.
make # compile the library
make clean # remove object files
make fclean # remove object files and library
make re # recompile from scratchTo use in your own project:
#include "ft_printf.h"
# link with: cc your_file.c libftprintf.aThe format string is parsed character by character in a single loop. When a % is encountered, the next character determines which conversion to apply via a dispatch function (switch_type). Each conversion reads the next variadic argument using va_arg and writes the output directly with write(), counting bytes as it goes. The function returns the total number of characters printed, mirroring the behavior of the original printf().
All logic lives in a single file. This approach trades reusability for compactness. Here are no shared helpers to extract across files, which made it easier to reason about the whole implementation at once while learning. The tradeoff is that functions like character output and string output are tightly coupled, but for a project of this scope that is acceptable.
Numbers are printed recursively: divide by the base, recurse on the quotient, then print the remainder digit. This avoids needing a buffer to reverse digits manually. Hexadecimal and unsigned decimal share the same recursive function (put_hex), parameterized by base and case.
- va_list in C — Exploring ft_printf
man 3 printf,man 3 stdarg- Tester Francinette
AI usage: Claude was used as a learning tool to understand variadic argument mechanics and to help write this README file, in accordance with the 42 global AI usage policy.