A reimplementation of the standard C
printf()function written from scratch, designed to demonstrate variadic functions, formatted output processing, and low-level memory handling.
ft_printf is one of the fundamental projects of the 42 curriculum, where the goal is to recreate the behavior of the standard printf function using only a limited subset of the C standard library.
- Built with: C
- Focus: Recreating printf-like output formatting, variadic functions, and format parsing.
- What I learned: Modular design, parsing logic, variadic arguments, type handling, and careful output management.
Recreate the function:
int ft_printf(const char *format, ...);The function must:
-
Parse a format string
-
Detect format specifiers
-
Retrieve variadic arguments
-
Format and print the output correctly
-
Return the number of printed characters
| Specifier | Description |
|---|---|
| %c | Print a character |
| %s | Print a string |
| %p | Print a pointer address |
| %d | Print a decimal integer |
| %i | Print an integer |
| %u | Print an unsigned integer |
| %x | Print hexadecimal (lowercase) |
| %X | Print hexadecimal (uppercase) |
| %% | Print a percent sign |
ft_printf relies on variadic arguments, allowing functions to accept a variable number of parameters.
Example:
int ft_printf(const char *format, ...);Arguments are accessed using:
va_list args;
va_start(args, format);
va_arg(args, type);
va_end(args);
Flow:
ft_printf()
│
▼
parse format string
│
▼
detect %
│
▼
retrieve argument
│
▼
format output
│
▼
write to stdout
The algorithm scans the string character by character.
ft_printf("Hello %s %d", name, age);
H → print
e → print
l → print
l → print
o → print
(space)
% → specifier detected
s → print string
(space)
% → specifier detected
d → print integer
Each specifier triggers a dedicated conversion function.
Example architecture:
ft_printf
│
├── handle_char
├── handle_string
├── handle_pointer
├── handle_integer
├── handle_unsigned
├── handle_hex
└── handle_percent
Each handler:
-
Retrieves the argument using va_arg
-
Converts it to a printable format
-
Writes it using write()
FORMAT STRING
│
▼
ft_printf
│
▼
FORMAT PARSER
│
▼
SPECIFIER DETECTED
│
▼
ARGUMENT RETRIEVAL
│
▼
CONVERSION FUNCTION
│
▼
write() → STDOUT
ft_printf/
│
├── ft_printf.c
├── ft_printf.h
│
├── ft_putchar.c
├── ft_putstr.c
├── ft_putnbr.c
│
├── ft_puthex.c
├── ft_putptr.c
│
└── README.md
| Core Files | File Role |
|---|---|
| ft_printf.c | Main formatting engine |
| ft_printf.h | Function prototypes |
| ft_putchar.c | Character printing |
| ft_putstr.c | String printing |
| ft_putnbr.c | Integer printing |
| ft_puthex.c | Hexadecimal conversion |
| ft_putptr.c | Pointer printing |
#include "ft_printf.h"
int main(void)
{
ft_printf("Hello %s\n", "world");
ft_printf("Number: %d\n", 42);
ft_printf("Hex: %x\n", 255);
ft_printf("Pointer: %p\n", &main);
}Hello world
Number: 42
Hex: ff
Pointer: 0x7ffee4b6c9a0Compile using:
cc -Wall -Wextra -Werror *.c
Or with a Makefile:
make
-
Implementing printf from scratch requires solving several non-trivial problems:
-
Parsing formatted strings
-
Handling variadic arguments
-
Implementing number-to-string conversions
-
Managing hexadecimal and pointer formatting
-
Handling edge cases
-
Returning accurate printed length
-
Low-Level Programming
-
variadic functions
-
formatted output
-
system calls (write)
-
Memory Handling
-
stack argument management
-
integer conversion algorithms
-
Algorithm Design
-
parsing engines
-
format interpretation
-
Debugging
-
segmentation faults
-
incorrect format parsing
-
integer conversion bugs
-
variadic functions (stdarg.h)
-
formatted string parsing
-
integer-to-string conversion
-
hexadecimal representation
-
pointer formatting
GitHub:
https://github.com/wkratos77⭐ If you found this project interesting, feel free to explore the implementation.