This project has been created as part of the 42 curriculum by gvoelkne.
ft_printf#
Description#
Reimplementation of the C standard library's printf function. This project implements formatted output to stdout, supporting the following format specifiers:
%c- single character%s- string%d,%i- signed decimal integer%u- unsigned decimal integer%x- unsigned hexadecimal (lowercase)%X- unsigned hexadecimal (uppercase)%p- pointer address%%- literal percent sign
Instructions#
# Build the static library
make
# Clean object files
make clean
# Clean object files and library
make fclean
# Rebuild from scratch
make re
To use in another project, compile with the library:
cc your_file.c -L. -lftprintf -o your_program
Algorithm and Data Structures#
The implementation uses a modular approach:
-
Main loop (
ft_printf): Parses the format string, identifies format specifiers, and dispatches to appropriate handlers. -
Variadic arguments (
va_list): Usesstdarg.hto access variable arguments passed to the function. -
Type-specific handlers:
- Character and string output handled directly via
write()syscalls - Integer types converted to strings using
ft_ntoa(signed) andft_untoa(unsigned) - Pointers formatted as hexadecimal with
0xprefix viaft_putptr
- Character and string output handled directly via
-
Helper functions: Memory-safe utilities (e.g.
ft_calloc,ft_strdup) and string manipulation (e.g.ft_strlen,ft_strlower) support the core functionality.
The design prioritizes clarity and correctness over performance, with each format specifier delegated to a dedicated function.
Resources#
- printf(3) man page
- stdarg.h documentation
- 42 School subject materials on variadic functions
- Mistral AI as a mental sparring partner.