This project has been created as part of the 42 curriculum by pabartoc (42 Heilbronn).
ft_printf is a project from the 42 curriculum. The goal is to recode the well-known standard C library function printf from <stdio.h>.
The main objective of this project is to learn how to use variadic functions in C (<stdarg.h>) and to develop a deep understanding of formatted output, typecasting, and clean memory management. Since this project is built without dynamic memory allocation (malloc) and operates directly via the write system call, it is highly resource-efficient, fast, and modular – making it perfectly suited for seamless integration into your own libft.
The function has the following prototype:
int ft_printf(const char *format, ...);It supports the following format specifiers:
| Format | Description | Implementation Details |
|---|---|---|
%c |
Prints a single character (char). |
Uses write directly via ft_putchar. |
%s |
Prints a string (char *). |
Catches NULL pointers and prints (null). |
%p |
Prints a memory address (void *) in hexadecimal. |
Adds the 0x prefix and handles NULL pointers platform-specifically (e.g., (nil) or 0x0). |
%d |
Prints a signed decimal number (base 10). | Handles INT_MIN (-2147483648) specifically to prevent overflows. |
%i |
Prints an integer in base 10. | Identical behavior to %d for output. |
%u |
Prints an unsigned decimal number (unsigned int). |
Processes values up to UINT_MAX (4294967295) using recursion. |
%x |
Prints a number in hexadecimal (base 16) in lowercase. | Uses the base "0123456789abcdef". |
%X |
Prints a number in hexadecimal (base 16) in uppercase. | Uses the base "0123456789ABCDEF". |
%% |
Prints a single percent sign %. |
Escapes the formatting character. |
Return value: Just like the original, ft_printf returns the total number of characters printed as an int (or -1 if the write system call fails).
The project is modularly divided into source code (src) and header files (inc) to ensure easy integration into larger projects (like libft):
ft_printf/
├── .gitignore
├── Makefile
├── README.md
├── inc/
│ └── ft_printf.h # Header file containing macros, includes & prototypes
└── src/
├── ft_printf.c # Main logic, parsing the format string & va_arg handling
└── ft_printf_add_ons.c # Helper functions for output (putchar, putstr, putnbr, putptr, puthex)
- A C compiler (e.g.,
cc,gcc, orclang) make
Clone the repository and run make in the root directory:
git clone https://github.com/pyoskel/printf.git ft_printf
cd ft_printf
makeThis will create the static library libftprintf.a in the root directory.
makeormake all: Compiles the.cfiles into.oobject files and archives them intolibftprintf.a.make clean: Deletes all generated.oobject files in thesrc/directory.make fclean: Executescleanand also deletes the librarylibftprintf.a.make re: Deletes everything (fclean) and recompiles the project from scratch (all).
To use ft_printf in your own code, include the header and compile your program alongside libftprintf.a.
#include "ft_printf.h"
#include <stdio.h>
int main(void)
{
int count_ft;
int count_orig;
int num = 42;
ft_printf("--- Test Start ---\n");
count_ft = ft_printf("ft_printf : Hello %s! Number: %d, Hex: %X, Ptr: %p, Percent: %%\n", "World", num, num, &num);
count_orig = printf("printf : Hello %s! Number: %d, Hex: %X, Ptr: %p, Percent: %%\n", "World", num, num, &num);
ft_printf("Return ft_printf: %d | Return printf: %d\n", count_ft, count_orig);
return (0);
}Since the header file is located in the inc/ folder, we pass the -I inc flag to the compiler:
# 1. Build the library
make
# 2. Compile main.c with the library
cc main.c libftprintf.a -I inc -o test_printf
# 3. Run the program
./test_printfWhen testing your implementation, pay special attention to:
- Passing
NULLto%sand%p. - Printing
INT_MIN(-2147483648) andINT_MAX. - Handling empty strings
""and single character strings. - Edge cases with
0for hex conversions (%x,%X,%p).
Francinette is a popular testing framework among 42 students. To test your project:
- Make sure Francinette is installed on your system.
- Navigate to your
ft_printfroot directory. - Run the strict check:
(Ensure your custom
paco -s
mainfunction is deleted or commented out before runningpaco, otherwise it will result in a compilation error!)
Since ft_printf does not use malloc, there shouldn't be any memory leaks. To verify this using your own test_printf executable:
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes ./test_printfYou should see All heap blocks were freed -- no leaks are possible in the Valgrind summary.