Skip to content
abdullahsmatrixPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

ft_printf - Architecture Documentation

Project Overview

A custom implementation of the C standard library's printf() function. It formats and prints output to standard output, supporting multiple format specifiers.


Design Architecture

Entry Point

ft_printf(format, ...)
  • Main function accepting format string and variadic arguments
  • Returns total character count printed

Processing Flow

ft_printf()
    ↓
    Format String Parser (scan for '%' specifiers)
    ↓
    format_specifier() (static helper)
    ↓
    va_list argument extractor
    ↓
    Specific Output Functions (based on specifier)
    ↓
    ft_putchar_fd() (base output function)

Function Hierarchy

Level 1: Main Entry Point

  • ft_printf(const char *format, ...) - Parses format string, routes specifiers, accumulates output count

Level 2: Dispatcher

  • format_specifier(char, va_list*) - Routes to appropriate output function based on specifier type

Level 3: Output Functions (Output Handlers)

Direct Output (writes 1 character)

  • ft_putchar_fd(char c) - Base function, writes single character to stdout

String Output

  • ft_putstr_fd(char *c) - Writes entire string; handles NULL with "(null)"
    • Calls: ft_putchar_fd() for each character

Numeric Output

  • ft_putnbr_fd(int n) - Converts signed integer to decimal; handles negative

    • Calls: ft_putchar_fd() for each digit (recursive decomposition)
  • ft_putnbr_u_fd(unsigned int n) - Converts unsigned integer to decimal

    • Calls: ft_putchar_fd() for each digit (recursive decomposition)

Hexadecimal Output

  • print_hex_l(unsigned long nbr) - Converts to lowercase hex (0-9, a-f)

    • Calls: ft_putchar_fd() for each hex digit (recursive decomposition)
  • print_hex_u(unsigned long nbr) - Converts to uppercase hex (0-9, A-F)

    • Calls: ft_putchar_fd() for each hex digit (recursive decomposition)

Pointer Output

  • print_pointer(void *ptr) - Formats pointer address as "0x..." in hex
    • Calls: ft_putstr_fd() and print_hex_l()

Supported Format Specifiers

Specifier Type Handler Function
%c Character ft_putchar_fd()
%s String ft_putstr_fd()
%p Pointer print_pointer()
%d / %i Signed Integer ft_putnbr_fd()
%u Unsigned Integer ft_putnbr_u_fd()
%x Hex (lowercase) print_hex_l()
%X Hex (uppercase) print_hex_u()
%% Literal % ft_putchar_fd('%')

Data Flow Example

Input: ft_printf("Hello %d %x\n", 42, 255)

ft_printf("Hello %d %x\n", 42, 255)
    ↓ Parse "Hello " → ft_putchar_fd() × 6
    ↓ Detect '%d' → format_specifier('d', &args)
    ↓ va_arg(args, int) → 42
    ↓ ft_putnbr_fd(42) → recursively outputs "42" (3 chars)
    ↓ Parse " " → ft_putchar_fd() × 1
    ↓ Detect '%x' → format_specifier('x', &args)
    ↓ va_arg(args, unsigned int) → 255
    ↓ print_hex_l(255) → recursively outputs "ff" (2 chars)
    ↓ Parse "\n" → ft_putchar_fd() × 1
    ↓ Return 13 (total characters printed)

Key Implementation Details

  • Base Function: All output ultimately calls ft_putchar_fd() using write(1, &ch, 1)
  • Return Values: Each function returns count of characters written (for accumulation)
  • Recursion: Number-to-string conversion uses recursion to process digits from most-significant to least
  • NULL Handling: String function handles NULL pointers with "(null)"; pointer function uses "(nil)"

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages