A custom printf implementation built as a static C library for the 42 curriculum.
variadic arguments • formatted output • static library • write(2)
ft_printf is a project developed as part of the 42 curriculum.
In the current repository state, the project builds libftprintf.a and implements a subset of the standard printf behavior. The dispatcher in ft_printf.c currently supports %c, %s, %p, %d, %i, %u, %x, %X, and %%, writes to standard output, and returns the number of printed characters.
This project focuses on:
- variadic argument handling with
va_list - parsing format specifiers inside a format string
- converting signed, unsigned, hexadecimal, and pointer values
- packaging the implementation as a reusable static library
- ✅ Supports
%c,%s,%p,%d,%i,%u,%x,%X, and%% - ✅ Returns the number of printed characters, like
printf - ✅ Handles
NULLstrings by printing(null) - ✅ Prints pointer addresses with the
0xprefix - ✅ Supports both lowercase and uppercase hexadecimal output
⚠️ The current implementation does not handle flags, width, or precision
Through this project, the following concepts are practiced:
- variadic functions with
va_start,va_arg, andva_end - low-level output with
write - recursive number printing
- base 10 and base 16 conversion
- static library creation with
ar - API exposure through a public header
From the project root, compile the library with:
makeAvailable Makefile rules:
make
make clean
make fclean
make reAdditional notes:
- The default target creates
libftprintf.a. main.cis a local test file and is not included in the library build.- The files compiled into the library are the ones listed in
FT_PRINTFinsideMakefile.
Since the repository builds a library, the simplest way to try it is to link it with the provided main.c test file:
make
cc -Wall -Wextra -Werror main.c libftprintf.a -I ./ -o test_printf
./test_printfcc -Wall -Wextra -Werror main.c libftprintf.a -I ./ -o test_printf
./test_printf
cc -Wall -Wextra -Werror your_file.c libftprintf.a -I ./ -o your_programAdditional notes:
- Public function:
int ft_printf(const char *type, ...); - Supported conversions in the current code are
%c,%s,%p,%d,%i,%u,%x,%X, and%%.
.
├── Makefile
├── ft_printf.h
├── ft_printf.c
├── ft_putchar.c
├── ft_putstr.c
├── ft_putnbr2.c
├── ft_printunsigned_decimal.c
├── ft_printhexa_low.c
├── ft_printhexa_upper.c
├── ft_printadresse.c
├── main.c
├── ft_putnbr.c
├── ft_printmodulo.c
├── printf.h
├── template.md
└── README.md
- Repository root: flat layout containing sources, headers, the Makefile, and local test files
ft_printf.c: main parser and dispatcher for supported format specifiersft_putchar.c,ft_putstr.c,ft_putnbr2.c,ft_printunsigned_decimal.c,ft_printhexa_low.c,ft_printhexa_upper.c,ft_printadresse.c: helper functions used by the library buildmain.c: manual comparison file against the standardprintfft_putnbr.c,ft_printmodulo.c, andprintf.h: present in the repository but not referenced by the currentSRCSlist inMakefile
for this project:
- handles important edge cases such as NULL strings, NULL pointers, and INT_MIN
- reuses generic helpers to avoid duplicated logic, especially for hexadecimal output
- built with a simple and readable parsing flow
- designed to stay compliant with 42 project constraints and Norm rules
- limits its implemented scope to the conversion set listed above
The current repository supports the following testing approach:
- library build with
make - manual comparison against the standard
printfviamain.c - output checks for
%c,%s,%p,%x,%X,%d,%u, and%% - return value checks printed by the local test program
- basic
NULL, zero, and unsigned edge-case coverage in the provided examples
make
cc -Wall -Wextra -Werror main.c libftprintf.a -I ./ -o test_printf
./test_printfvalgrind --leak-check=full --track-origins=yes ./test_printfA project like this is useful for improving in the following areas:
- thinking in terms of parsing and dispatch
- separating formatting responsibilities into focused helper functions
- reasoning about returned character counts, not only printed output
- building and linking a reusable static library
Although the current repository builds and runs, several improvements could be considered:
- add support for flags, width, precision, and additional specifiers
- add automated regression tests instead of relying only on manual comparison
- reorganize the repository into
src/andincludes/directories - remove or consolidate files that are not part of the current build
kpourcel
42 student
Makefilefor the build rules and compiled source listmain.cfor the manual testing workflow and usage examplesft_putstr.candft_printadresse.cforNULLoutput behavior details