This project has been created as part of the 42 curriculum by ebin-ahm.
To recode printf function from scratch, stylised as ft_printf in accordance to 42 standard without using the internal buffering system of the original printf, while supporting all mandatory format specifiers.
printf is a key function in C that takes in a format string, prints and output a formatted string . printf reads a string, character by character, while interpreting % as format specifiers.
Each format specifier tells printf what type of argument to retrieve from the variadic argument list (using va_list, va_start, va_arg, and va_end) and how to convert that value into text.
| Specifier | Description |
|---|---|
%c |
Print a single character |
%s |
Print a string |
%p |
Print a pointer address (0x + hex) |
%d / %i |
Print a signed integer |
%u |
Print an unsigned integer |
%x |
Print a lowercase hexadecimal number |
%X |
Print an uppercase hexadecimal number |
%% |
Print a literal % character |
The final output is written to standard output, and printf returns the total number of characters printed.
Makefile – builds the libftprintf.a library
ft_printf.h – contains function prototypes and required headers
ft_printf.c – parses the format string and handles variadic arguments
print_*.c files – handle printing for each format specifierr
To compile the library, run:
makeTo remove object files:
make cleanTo remove object files AND the library:
make fcleanTo rebuild everything:
make recc -Wall -Wextra -Werror main.c libftprintf.a -o program
./program
ft_printf/ ├── Makefile ├── README.md ├── ft_printf.h ├── ft_printf.c ├── print_char.c ├── print_string.c ├── print_int.c ├── print_unsigned.c ├── print_hex.c ├── print_pointer.c └── print_percent.c
Each print_.c file implements the logic for a specific format specifier, keeping the design modular and easy to maintain.*
-
ft_printfiterates through the format string character by character while managing a va_list. -
Regular characters are written directly to standard output using
write()and counted. -
When a % is encountered, the next character is treated as a format specifier and dispatched to the appropriate handler using
va_arg. -
Each handler prints its value (handling special cases such as NULL) and returns the number of characters printed, which is added to the total count.
-
After processing the entire format string, va_end is called and ft_printf returns the total number of characters written.
- man pages for standard library functions
- 42 Norminette rules
- 42 ft_printf PDF version 12.0
AI (ChatGPT) was used for:
- Clarifying concepts such as pointers, memory operations, and linked list operations
- Explaining algorithms, edge cases, and debugging
All code was manually written and verified by ebin-ahm.