My solution for the ft_printf assignment.
0

Configure Feed

Select the types of activity you want to include in your feed.

63 1 0

Clone this repository

https://tangled.org/gm0stache.eu/ft-printf https://tangled.org/did:plc:jlslkk2fdbym6ralknh5x2gk
git@tangled.org:gm0stache.eu/ft-printf git@tangled.org:did:plc:jlslkk2fdbym6ralknh5x2gk

For self-hosted knots, clone URLs may differ based on your setup.



README.md

This project has been created as part of the 42 curriculum by gvoelkne.

ft_printf#

Description#

Reimplementation of the C standard library's printf function. This project implements formatted output to stdout, supporting the following format specifiers:

  • %c - single character
  • %s - string
  • %d, %i - signed decimal integer
  • %u - unsigned decimal integer
  • %x - unsigned hexadecimal (lowercase)
  • %X - unsigned hexadecimal (uppercase)
  • %p - pointer address
  • %% - literal percent sign

Instructions#

# Build the static library
make

# Clean object files
make clean

# Clean object files and library
make fclean

# Rebuild from scratch
make re

To use in another project, compile with the library:

cc your_file.c -L. -lftprintf -o your_program

Algorithm and Data Structures#

The implementation uses a modular approach:

  1. Main loop (ft_printf): Parses the format string, identifies format specifiers, and dispatches to appropriate handlers.

  2. Variadic arguments (va_list): Uses stdarg.h to access variable arguments passed to the function.

  3. Type-specific handlers:

    • Character and string output handled directly via write() syscalls
    • Integer types converted to strings using ft_ntoa (signed) and ft_untoa (unsigned)
    • Pointers formatted as hexadecimal with 0x prefix via ft_putptr
  4. Helper functions: Memory-safe utilities (e.g. ft_calloc, ft_strdup) and string manipulation (e.g. ft_strlen, ft_strlower) support the core functionality.

The design prioritizes clarity and correctness over performance, with each format specifier delegated to a dedicated function.

Resources#