A lightweight reimplementation of printf from the 42 curriculum, built as a static library: libftprintf.a.
This version focuses on mandatory conversion specifiers and writes directly to standard output using write(2).
- Supports:
%c,%s,%p,%d,%i,%u,%x,%X,%% - Returns the total number of printed characters
- Handles
NULLstrings as(null) - Prints pointers in lowercase hexadecimal with
0xprefix - Built with strict flags:
-Wall -Wextra -Werror
ft_printf/
├── Makefile
├── ft_printf.h
├── ft_printf.c
├── ft_putchar.c
├── ft_putstr.c
├── ft_putnbr.c
├── ft_putnbr_un.c
├── ft_puthx.c
└── ft_putadrs.c
From ft_printf/:
makeThis generates:
libftprintf.a
Other targets:
make clean
make fclean
make reDeclared in ft_printf.h:
int ft_printf(const char *str, ...);Helper functions are also exposed in the header and used internally:
ft_putcharft_putstrft_putnbrft_putnbr_unft_puthxft_putadrs
| Specifier | Meaning |
|---|---|
%c |
character |
%s |
string |
%p |
pointer address (0x...) |
%d / %i |
signed decimal integer |
%u |
unsigned decimal integer |
%x |
unsigned hexadecimal (lowercase) |
%X |
unsigned hexadecimal (uppercase) |
%% |
literal % |
#include "ft_printf.h"If you are compiling from inside ft_printf/:
cc -Wall -Wextra -Werror main.c libftprintf.a -o appIf ft_printf/ is a subfolder of your project:
cc -Wall -Wextra -Werror main.c -L./ft_printf -lftprintf -I./ft_printf -o app#include "ft_printf.h"
int main(void)
{
int count;
count = ft_printf("Char: %c | Str: %s | Num: %d | Hex: %x | Ptr: %p | %%\n",
'A', "hello", -42, 255, "hello");
ft_printf("Printed chars: %d\n", count);
return (0);
}- Output is written to file descriptor
1. - Write errors propagate as
-1fromft_printf. - Unknown specifiers after
%are printed as-is (for example,%qprintsq). - A trailing
%at end of format string does not print anything and does not error. ft_putnbr_uninternally callsft_putnbrfor recursion steps; output remains correct for unsigned values.
This implementation does not include standard printf flags or advanced formatting features, such as:
- width
- precision
- left/right alignment
- zero padding
+, space,#flags- length modifiers (
hh,h,l,ll, etc.)
No license file is currently present in this folder. If you plan to share or reuse this project publicly, add a license file (for example, MIT).