ft_printf is a 42 project that recreates part of the behavior of the standard printf() function from libc.
It strengthens your skills in:
- handling variadic arguments (
va_list) - manual data formatting
- modular and clean C programming
- implementing flags and reproducing standard printf behavior
Implement the function:
int ft_printf(const char *format, ...);This function:
- Prints formatted output to stdout
- Supports several conversion specifiers
- Returns the number of characters printed
- Reproduces the behavior of the original printf for mandatory conversions
| Specifier | Meaning | Example |
|---|---|---|
%c |
Character | 'A' |
%s |
String | "Hello" |
%p |
Pointer (0x...) or (nil) |
(nil) |
%d |
Signed integer | -42 |
%i |
Signed integer | 24 |
%u |
Unsigned integer | 42 |
%x |
Hexadecimal (lowercase) | 2a |
%X |
Hexadecimal (uppercase) | 2A |
%% |
Prints % |
% |
- Only
write()may be used for printing - Return value = total printed characters
%pprints(nil)when the pointer is NULL- Code must follow the 42 Norm
This project implements the bonus flag set allowed by the subject:
| Flag | Description | Example | Output |
|---|---|---|---|
+ |
Always show the sign | %+d, 42 |
+42 |
(space) |
Leading space for positive numbers | % d, 42 |
42 |
# |
Hexadecimal prefix | %#x, 255 |
0xff |
- If
+andare both used →+wins #only affects%xand%X#does not add prefix when the number is 0
According to the subject:
You only need to implement one bonus set.
This project implements:#,+, and space.
ft_printf/
├── ft_printf.c
├── ft_printf.h
├── ft_printf_utils.c
├── ft_flags_bonus.c
├── libft/
│ ├── *.c
│ └── libft.a
└── Makefile
ft_printfiterates through the format string- When it finds
%, it checks:- flags
- conversion type
- It calls helper functions depending on the type
- It prints numbers, strings, characters, hex, and pointers manually
- It returns the number of characters printed
Build the library:
makeUse it in your program:
cc -Wall -Wextra -Werror main.c libftprintf.a -I .int main(void)
{
ft_printf("Char: %c\n", 'A');
ft_printf("String: %s\n", "Hello");
ft_printf("Pointer: %p\n", NULL);
ft_printf("Decimal: %d\n", -42);
ft_printf("Unsigned: %u\n", 4242);
ft_printf("Hex: %x / %X\n", 255, 255);
ft_printf("Percent: %%\n");
// Bonus flags
ft_printf("Plus flag: %+d\n", 42);
ft_printf("Space flag: % d\n", 42);
ft_printf("Hash flag: %#x\n", 255);
return (0);
}- Fully compliant with the 42 Norm
- No forbidden functions
- Bonus is evaluated only if mandatory part is perfect
%pmust print(nil)for null pointers- Every printed character must be counted precisely
This project is part of the 42 curriculum.
You may read and study the code — just make sure you truly understand it. ✨