ft_printf

A custom implementation of the standard C printf function, managing variadic arguments and complex string formatting.

2021-06-01

low-levelOpen GitHub
ft_printf

ft_printf

A reimplementation of printf in C without touching any formatted-output functions. It walks the format string character by character, and when it finds a % it dispatches to the right handler for that specifier.

Supported conversions

SpecifierOutput
%cSingle character
%sString (prints (null) for NULL pointers)
%pPointer in hex
%d / %iSigned decimal integer
%uUnsigned decimal integer
%xHex lowercase
%XHex uppercase
%%Literal percent sign

Signature

int ft_printf(const char *format, ...);

Returns the number of characters written, or -1 on error. Same contract as the real printf.

How it works

ft_printf("Dec: %d | Hex: %x\n", 42, 42)
                  |           |
                  |           ft_hexalow(42) -> "2a"
                  ft_putnbr(42) -> "42"

Usage

#include "ft_printf.h"
 
ft_printf("Hello, %s!\n", "world");
ft_printf("Dec: %d | Hex: %x | Ptr: %p\n", 255, 255, &main);
make
gcc main.c libftprintf.a -o program

SCREENSHOTS // 01 FRAMES

Screenshot 1
01 / 01