This project has been created as part of the 42 curriculum by opektas.
ft_printf is a partial reimplementation of the C standard library function printf. The goal is to understand how variadic functions work in C and how formatted output is produced without relying on the original function.
The function parses a format string character by character. When it encounters a % followed by a conversion specifier, it pulls the next argument from the variadic list and dispatches it to the appropriate output function. For all other characters, it writes them directly to stdout. The total number of characters written is tracked and returned.
Prototype:
int ft_printf(const char *format, ...);Supported conversion specifiers:
| Specifier | Output |
|---|---|
%c |
Single character |
%s |
String of characters ((null) if NULL) |
%p |
Pointer address in hexadecimal (0x..., (nil) if NULL) |
%d |
Signed decimal integer |
%i |
Signed integer in base 10 |
%u |
Unsigned decimal integer |
%x |
Unsigned hexadecimal integer (lowercase) |
%X |
Unsigned hexadecimal integer (uppercase) |
%% |
Literal % character |
The core logic is a simple linear scan of the format string using an index i. No intermediate buffer is used — each character or formatted argument is written to stdout immediately via write(1, ...). This keeps memory usage minimal and avoids any need for dynamic allocation.
When a % is detected, i is incremented and the next character is passed to ft_dispatch, a static dispatcher function that selects the correct output function based on the conversion type. Each output function returns the number of characters it wrote, which is accumulated in a count variable.
For numeric output, a single recursive function ft_putnbr_base handles all bases (decimal, hexadecimal) by dividing the number recursively until it reaches a single digit, then writing digits on the way back up the call stack. This avoids duplicating logic across %u, %x, and %X. Signed integers are handled separately in ft_putnbr, which prepends a - sign and negates the value before passing it to ft_putnbr_base.
makeProduces libftprintf.a in the project root.
cc -Wall -Wextra -Werror your_file.c libftprintf.a -o your_programInclude the header in your source:
#include "ft_printf.h"| Rule | Description |
|---|---|
make / make all |
Compile the static library |
make clean |
Remove object files |
make fclean |
Remove object files and the library |
make re |
Full rebuild |
#include "ft_printf.h"
int main(void)
{
int n = 42;
ft_printf("String: %s\n", "hello");
ft_printf("Integer: %d\n", n);
ft_printf("Hex: %x\n", 255);
ft_printf("Pointer: %p\n", &n);
return (0);
}- printf(3) — Linux manual page
- stdarg.h — cppreference
- va_start, va_arg, va_end — cppreference
- Variadic functions in C — GNU C Library
Claude was used in the following ways during this project:
- Learning
va_*functions: I asked Claude to explain howva_list,va_start,va_arg,va_copy, andva_endwork internally, and how variadic argument traversal works at the ABI level. This helped me understand whycharandshortarguments must be retrieved asintviava_arg. - Understanding
printfbehavior: I asked Claude to clarify edge case behavior of the realprintf, such as howNULLis handled for%sand%p, and the practical difference between%dand%i. - README writing assistance: I used Claude to help structure and phrase this README in English.