This project has been created as part of the 42 curriculum by yoben-ch.
ft_printf is a small C library that recreates the mandatory behavior of the standard printf function for the 42 curriculum. The project focuses on variadic functions, formatted output, integer base conversion, pointer formatting, and accurate return values.
The library builds as libftprintf.a and exposes this function:
int ft_printf(const char *format, ...);Supported mandatory conversions:
%cprints one character.%sprints a string, using(null)for a null pointer.%pprints a pointer in hexadecimal format, using(nil)for a null pointer on this platform.%dand%iprint signed decimal integers.%uprints unsigned decimal integers.%xprints lowercase hexadecimal integers.%Xprints uppercase hexadecimal integers.%%prints a percent sign.
Bonus flags, width, and precision are not implemented.
Compile the library:
makeThis creates libftprintf.a at the repository root.
Clean object files:
make cleanRemove object files and the library:
make fcleanRebuild from scratch:
make reExample usage:
#include "ft_printf.h"
int main(void)
{
ft_printf("Hello %s, value = %d\n", "42", 42);
return (0);
}Compile with the library:
cc -Wall -Wextra -Werror main.c libftprintf.a -I.The project uses a linear parser over the format string. Each normal character is written directly to standard output with write. When the parser finds a % followed by a supported conversion specifier, it dispatches to a small helper function for that conversion.
The main data structure is va_list, from <stdarg.h>, which stores the current position in the variable argument list. ft_printf initializes it with va_start, extracts arguments with va_arg, and releases it with va_end.
Numbers are printed recursively. For decimal output, the number is divided by 10 until the most significant digit is reached, then digits are written while the recursion unwinds. Unsigned decimal uses the same idea without sign handling. Hexadecimal output uses the same recursive division strategy with base 16 and a base string (0123456789abcdef or 0123456789ABCDEF) to choose each digit.
This approach avoids dynamic allocation, keeps memory management simple, and matches the subject requirement not to reproduce libc printf buffer management. Each helper returns the number of characters it wrote, and ft_printf accumulates those values to produce the final return count.
Useful references for this topic:
man 3 printfman 3 stdargman 2 write- The 42 ft_printf subject PDF included in this repository
- cppreference: Variadic arguments in C
- The GNU C Library manual: Formatted Output
AI use: AI assistance was used to read and summarize the subject requirements, run build and behavior checks, and draft this README. The implementation files were reviewed against the subject, but no bonus behavior was added.