ft_printf is a custom implementation of the standard C printf function.
The project introduces variadic functions and formatted output processing by recreating the core behavior of printf without relying on the original implementation.
It was completed as part of the 42 / 1337 Common Core.
The function supports the following conversions:
| Conversion | Description |
|---|---|
%c |
Prints a single character |
%s |
Prints a string |
%p |
Prints a pointer address in hexadecimal |
%d |
Prints a signed decimal integer |
%i |
Prints a signed integer |
%u |
Prints an unsigned decimal integer |
%x |
Prints a hexadecimal integer using lowercase letters |
%X |
Prints a hexadecimal integer using uppercase letters |
%% |
Prints a percent sign |
%o |
Prints an unsigned integer in octal |
int ft_printf(const char *format, ...);The function returns the total number of characters written, matching the behavior of the standard printf function.
Clone the repository:
git clone https://github.com/wkratos/ft_printf.git
cd ft_printfBuild the static library:
makeThis creates:
libftprintf.a
Other available rules:
make clean
make fclean
make reInclude the header in your program:
#include "ft_printf.h"Example:
#include "ft_printf.h"
int main(void)
{
int count;
count = ft_printf(
"Character: %c\nString: %s\nNumber: %d\nHex: %#x\n",
'A',
"Hello, 42!",
1337,
1337
);
ft_printf("Printed characters: %d\n", count);
return (0);
}Compile your program with the library:
cc -Wall -Wextra -Werror main.c libftprintf.a -o ft_printf_testRun it:
./ft_printf_testThe implementation processes the format string one character at a time.
When a % character is found, the conversion identifier is parsed and dispatched to the appropriate output function.
The project includes logic for:
- variadic argument handling with
va_list; - signed and unsigned number conversion;
- hexadecimal and octal conversion;
- pointer formatting;
- prefix and sign handling;
- accurate return-value calculation;
- safe handling of null strings.
.
├── Makefile
├── ft_printf.c
├── ft_printf.h
├── source files
└── README.md
The exact file organization may differ depending on the implementation.
You can compare the output and return value of ft_printf with the original printf:
#include <stdio.h>
#include "ft_printf.h"
int main(void)
{
int original;
int custom;
original = printf("Original: %#x %+d %s\n", 42, 42, "test");
custom = ft_printf("Custom: %#x %+d %s\n", 42, 42, "test");
printf("printf returned: %d\n", original);
printf("ft_printf returned: %d\n", custom);
return (0);
}Recommended cases to test include:
- zero;
- positive and negative integers;
INT_MINandINT_MAX;UINT_MAX;- null strings;
- null pointers;
- consecutive conversions;
- literal percent signs;
- hexadecimal and octal prefixes.
This project provided practical experience with:
- variadic functions;
- formatted output parsing;
- number-base conversion;
- static libraries;
- modular C programming;
- edge-case handling;
- reproducing standard-library behavior.
Walid Krati
- GitHub: @wkratos
This repository contains my implementation of the 42 ft_printf project. It is intended as a portfolio and educational reference. Students are encouraged to understand and implement the project independently.