← Back to projects

ft_printf

Reimplementation of printf in C, with variadic arguments and its own format parser.

Period
10/03/2022
Category
42
Area
Systems
Grade
125
Stack
  • C
Links

Rewriting one of the best-known functions of the C language, format parser included.

Conversions

%c for a character, %s for a string, %p for a pointer in hexadecimal, %d and %i for a signed decimal, %u for an unsigned one, %x and %X for hexadecimal, and %% for a literal percent sign.

Flags

The bonus part adds a minimum field width on every conversion, and any combination of - to left-justify, 0 to pad with zeroes, . for precision, # for the 0x prefix on hexadecimal, a leading space on positive numbers and + to force the sign.

What it taught me

Variadic functions and va_arg, but above all how to keep a small parser readable: one pass over the format string, one dispatch per conversion, and a return value that counts exactly the characters written.