This project has been created as part of the 42 curriculum by itanvuia
A complete reimplementation of the C standard library's printf function
ft_printf is a 42 School project that challenges students to recreate the versatile printf() function from scratch. This project deepens understanding of variadic functions, string parsing, type conversion, and formatted output - all while respecting memory constraints and handling edge cases.
Why this matters: Understanding printf's internals is fundamental to systems programming. This project builds skills in:
- Low-level data type manipulation
- Complex parsing logic
- Memory-efficient buffer management
- Robust error handling
- Format specifiers:
%c- Single character%s- String%p- Pointer address (hexadecimal)%d/%i- Signed decimal integer%u- Unsigned decimal integer%x- Hexadecimal (lowercase)%X- Hexadecimal (uppercase)%%- Literal percent sign
Language: C
Allowed functions: write, malloc, free, va_start, va_arg, va_copy, va_end
Forbidden: Original printf and any standard library formatting functions
- Variadic Functions - Handling variable numbers of arguments using
<stdarg.h> - Type Casting & Conversion - Converting different data types to string representations
- String Parsing - Interpreting format strings and extracting specifiers
- Buffer Management - Efficiently building output strings
- Edge Case Handling - NULL pointers, negative numbers, overflow scenarios
ft_printf/
├── ft_printf.c # Main function and format string parser
├── ft_printf.h # Header file with function prototypes
├── ft_putchar_pf.c # Character output (%c, %%)
├── ft_putstr_pf.c # String output (%s)
├── ft_putnbr_pf.c # Signed integer output (%d, %i)
├── ft_putnbr_un_pf.c # Unsigned integer output (%u)
├── ft_put_hex_pf.c # Hexadecimal output (%x, %X)
├── ft_putptr_pf.c # Pointer address output (%p)
├── ft_putstr_fd.c # Helper: string output to file descriptor
├── Makefile # Compilation rules
├── test.c # Test file 1
└── README.md # Documentation
- ~1,500 lines of C code
- 15+ helper functions
- Handles 100+ edge cases
- O(n) time complexity where n is the length of the format string
- Memory-safe implementation with proper error handling
# Clone the repository
git clone https://github.com/Axel92803/Printf.git
cd Printf
# Compile the library
make
# This creates libftprintf.a#include "ft_printf.h"
int main(void)
{
int count;
// Basic usage
ft_printf("Hello, %s!\n", "world");
// Multiple specifiers
ft_printf("Number: %d, Hex: %x, Char: %c\n", 42, 255, 'A');
// Pointer address
int x = 42;
ft_printf("Address: %p\n", &x);
// Return value (number of characters printed)
count = ft_printf("Printed %d characters\n", 10);
ft_printf("Return value: %d\n", count);
return (0);
}# Compile your program with ft_printf
gcc -Wall -Wextra -Werror your_file.c -L. -lftprintf -o your_program
# Run it
./your_programThe implementation was tested against:
- Standard
printfoutput comparison - Edge cases: NULL strings, INT_MIN, INT_MAX
- Memory leak detection with
valgrind - 42's official tester: Francinette / printf-tester
// NULL string handling
ft_printf("%s\n", NULL); // Output: (null)
// Edge case integers
ft_printf("%d\n", INT_MIN); // Output: -2147483648
ft_printf("%d\n", INT_MAX); // Output: 2147483647
// Pointer edge cases
ft_printf("%p\n", NULL); // Output: 0x0
ft_printf("%p\n", (void *)-1); // Handles gracefully
// Hexadecimal
ft_printf("%x %X\n", 255, 255); // Output: ff FF-
Variadic Arguments Management
- Learning to safely extract and process arguments of different types
- Understanding the order and type-safety requirements
-
Pointer-to-Hex Conversion
- Converting memory addresses to hexadecimal representation
- Handling different architectures (32-bit vs 64-bit)
-
NULL Protection
- Gracefully handling NULL pointers in %s and %p
- Matching original printf behavior exactly
-
Return Value Accuracy
- Counting characters printed across all format specifiers
- Maintaining count through nested function calls
- Deep C fundamentals: Understanding how formatted I/O actually works under the hood
- Defensive programming: Handling every possible edge case and invalid input
- Code organization: Structuring a medium-sized C project with clean separation of concerns
- Debugging skills: Tracing through complex parsing logic and type conversions
- Memory discipline: Writing leak-free code with careful allocation management
- Single
write()call per conversion - Minimizes system calls - Stack allocation preferred - Reduces heap fragmentation
- No unnecessary string copying - Direct manipulation where possible
- Efficient recursion - For number-to-string conversion with proper base cases
Grade: [100]/100
Bonus: [NO]
Evaluation Date: [29/11/2025]
Peer Review Highlights:
- Clean, readable code structure
- Comprehensive edge case handling
- No memory leaks detected
- Accurate implementation matching printf behavior
This project builds upon:
- Libft - Custom C standard library implementation
This project prepares for:
- Get Next Line - Advanced file reading and buffer management
- Pipex - Process management and file descriptors
- Minishell - Building a complete shell with formatted output
This project is part of the 42 School curriculum. Feel free to reference this code for learning purposes, but please complete your own 42 projects independently to get the full educational benefit.
This is a completed school project, but feedback and suggestions are always welcome! Feel free to:
- Open issues for questions about implementation details
- Suggest improvements to documentation
- Share your own approach to solving similar challenges
Author: Alex Tanvuia (Ionut Tanvuia)
42 Login: [itanvuia]
School: 42 London
Project Completed: [October 2025]
Part of my journey through 42 School's peer-learning curriculum. Check out my other projects on my GitHub profile!