This project has been created as part of the 42 curriculum by nkerstin.
ft_printf is a custom re-implementation of the standard C printf function as part of the 42 school curriculum. The goal is to recreate the variadic formatted-output function from scratch, supporting a defined subset of the original conversion specifiers, and packaging it as a static library that can be reused throughout future 42 projects.
The mandatory part covers the following conversions:
| Specifier | Description |
|---|---|
%c |
Prints a single character |
%s |
Prints a string (or (null) if the pointer is NULL) |
%d / %i |
Prints a signed decimal integer |
%u |
Prints an unsigned decimal integer |
%x |
Prints an unsigned number in lowercase hexadecimal |
%X |
Prints an unsigned number in uppercase hexadecimal |
%p |
Prints a pointer address as 0x followed by lowercase hex (or (nil) if the pointer is NULL) |
%% |
Prints a literal % character |
int ft_printf(const char *format, ...);ft_printf parses the format string, consumes the corresponding variadic arguments and writes the result to the standard output (file descriptor 1/STDOUT_FILENO). It returns the total number of bytes written, mirroring the behavior of the original printf.
The conversions are implemented by a set of helpers. The main helper is ft_putbase, which is also used in ft_putptr. All helpers are defined in the header and implementet in the file: printf_utils.c
| Function | Description |
|---|---|
ft_putchar |
Writes a single character to stdout |
ft_putstr |
Writes a string (handles NULL) |
ft_putptr |
Writes a pointer in the 0x... format |
ft_putbase |
Writes a number in a given base (10, 16 lowercase, 16 uppercase) |
Every helper returns the number of bytes it wrote, so the caller can simply accumulate the count.
ft_fmt is a small dispatcher: it receives the va_list by pointer (so the iteration state is shared with ft_printf and not copied), inspects the conversion character, pulls the matching argument with va_arg, and forwards it to the right helper while returning the number of bytes written. The negative %d / %i case is handled here by promoting to long before negation, which avoids the INT_MIN overflow that would occur with plain int negation.
ft_putbase is a single recursive routine that converts an unsigned long into any base described by a digit lookup string ("0123456789", "0123456789abcdef", or "0123456789ABCDEF"). It first recurses on n / base_len so that the most-significant digit reaches write first, then prints base_string[n % base_len]; reusing the same function for %u, %x, %X and the hex tail of %p keeps the conversion logic in one place instead of duplicating it per specifier.
unsigned long was chosen because on the 64-bit Linux machines used at 42 it is wide enough to hold any pointer value cast through it for %p, while still covering the 32-bit unsigned int range needed by %u, %x and %X. This project has only been tested on 64-bit Linux; a strictly portable implementation would use uintptr_t for the pointer path instead of relying on sizeof(unsigned long) == sizeof(void *).
- A C compiler:
cc make
Compile the library with:
makeThis produces libftprintf.a, the static library archive that can then be linked into other projects.
| Rule | Action |
|---|---|
make / make all |
Compiles the library |
make clean |
Removes object files |
make fclean |
Removes object files and libftprintf.a |
make re |
Runs fclean then all |
#include "ft_printf.h"
int main(void)
{
int n = ft_printf("Hello %s, the answer is %d (0x%x)\n", "world", 42, 42);
ft_printf("Wrote %d bytes\n", n);
return (0);
}man 3 printf— the reference behavior being re-implementedman 3 stdarg—va_list,va_start,va_arg,va_end- Stack Overflow
- Anakin's Github — understanding
ft_puthex/ft_putbase
AI tools (mostly Claude) were used during this project for the following purposes:
- Understanding function behavior and edge cases: Clarifying the behavior of
printfforNULLstrings,NULLpointers,INT_MIN, and the difference between Linux and BSD%poutput. - Debugging: Describing unexpected output, segfaults or wrong byte counts and getting guidance on likely causes.
va_listsemantics: Confirming whyva_listshould be forwarded by pointer rather than by value across helper functions.- README drafting: Structuring and formatting this documentation file.
AI was not used to write or generate any C source code submitted for evaluation.