A custom implementation of the C standard library's printf() function. It formats and prints output to standard output, supporting multiple format specifiers.
ft_printf(format, ...)
- Main function accepting format string and variadic arguments
- Returns total character count printed
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)
ft_printf(const char *format, ...)- Parses format string, routes specifiers, accumulates output count
format_specifier(char, va_list*)- Routes to appropriate output function based on specifier type
ft_putchar_fd(char c)- Base function, writes single character to stdout
ft_putstr_fd(char *c)- Writes entire string; handles NULL with "(null)"- Calls:
ft_putchar_fd()for each character
- Calls:
-
ft_putnbr_fd(int n)- Converts signed integer to decimal; handles negative- Calls:
ft_putchar_fd()for each digit (recursive decomposition)
- Calls:
-
ft_putnbr_u_fd(unsigned int n)- Converts unsigned integer to decimal- Calls:
ft_putchar_fd()for each digit (recursive decomposition)
- Calls:
-
print_hex_l(unsigned long nbr)- Converts to lowercase hex (0-9, a-f)- Calls:
ft_putchar_fd()for each hex digit (recursive decomposition)
- Calls:
-
print_hex_u(unsigned long nbr)- Converts to uppercase hex (0-9, A-F)- Calls:
ft_putchar_fd()for each hex digit (recursive decomposition)
- Calls:
print_pointer(void *ptr)- Formats pointer address as "0x..." in hex- Calls:
ft_putstr_fd()andprint_hex_l()
- Calls:
| 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('%') |
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)
- Base Function: All output ultimately calls
ft_putchar_fd()usingwrite(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)"