A lightweight, 42-style reimplementation of the C standard library's printf, written in C and built as a static library.
This project provides a drop-in function ft_printf with a subset of standard printf features commonly required in the 42 curriculum.
- Language: C
- Build system: Makefile
- Function signature:
int ft_printf(const char *format, ...);
- Returns the number of characters printed.
- Writes to standard output (file descriptor 1).
- Supported conversion specifiers:
%c— character%s— string%p— pointer (hexadecimal with0xprefix)%d,%i— signed decimal integer%u— unsigned decimal integer%x— unsigned hexadecimal (lowercase)%X— unsigned hexadecimal (uppercase)%%— literal percent sign
- A C toolchain (e.g.,
gccorclang) make
From the project root:
makeCommon Makefile targets:
make— build the librarymake clean— remove object filesmake fclean— remove objects and the librarymake re— rebuild from scratch
-
Include the header in your source:
#include "ft_printf.h"
-
Compile and link against the static library. Example:
gcc -Wall -Wextra -Werror -I./include -c main.c gcc -o app main.o -L. -lftprintf # or simply gcc -Wall -Wextra -Werror main.c ./libftprintf.a -I./include
#include "ft_printf.h"
int main(void)
{
int printed = ft_printf("Hello, %s! Number: %d Hex: %x %%\n", "world", 42, 0x2A);
ft_printf("Characters printed above: %d\n", printed);
return 0;
}Build:
gcc -Wall -Wextra -Werror main.c ./libftprintf.a -I./include -o demo
./demo- The return value accumulates the count of characters successfully written.
- Pointer values are printed with a
0xprefix followed by lowercase hexadecimal digits.
Quick comparison test:
int a = ft_printf("Mine => %d %x %p %%\n", 42, 42, &a);
int b = printf ("Sys => %d %x %p %%\n", 42, 42, &a);
printf("ft_printf count: %d | printf count: %d\n", a, b);Compile:
gcc -Wall -Wextra -Werror test.c ./libftprintf.a -I./include -o test
./test- Compile with
-Wall -Wextra -Werror. - Keep functions small and focused on a single conversion responsibility.
- Consider adding internal helper functions for hexadecimal and pointer formatting.