"Why use standard printf when you can recreate the entire functionality from scratch?" - Every 1337/42 Student Do π
Welcome to my implementation of ft_printf
- a custom printf function that replicates the behavior of the standard C library's printf()
. This project is all about diving deep into variadic functions, type handling, and the intricate world of formatted output.
- Full Printf Functionality: Supports multiple format specifiers
- Robust Error Handling: Carefully managed edge cases
- Recursive Implementation: Elegant and memory-efficient approach
- Extensible Design: Easy to understand and modify
- No Standard Library Shortcuts: Pure C implementation
Specifier | Description | Example |
---|---|---|
%c |
Single character | ft_printf("%c", 'A') |
%s |
String | ft_printf("%s", "Hello") |
%d |
Signed decimal integer | ft_printf("%d", 1337) |
%i |
Signed decimal integer | ft_printf("%i", -17) |
%u |
Unsigned decimal integer | ft_printf("%u", 100) |
%x |
Lowercase hexadecimal | ft_printf("%x", 255) |
%X |
Uppercase hexadecimal | ft_printf("%X", 255) |
%p |
Pointer address | ft_printf("%p", &var) |
%% |
Percent sign | ft_printf("%%") |
- Variadic Function (Recommended Resources Below)
- GCC compiler
- Make
Below is a list of resources to help you master this topic, from detailed explanations to practical implementations.
- π₯ Duration: Approx. 17 minutes
- π Overview: Explores the basics of variadic functions, their syntax, and their practical applications. Perfect for beginners who enjoy a straightforward teaching style.
- π₯ Duration: Approx. 13 minutes
- π οΈ Overview: A more advanced look at variadic functions, focusing on real-world coding examples and detailed implementation tips.
- π Type: Article
- π Overview: Covers the theoretical foundation, with code examples illustrating how variadic functions work. Includes topics like
stdarg.h
and the use of macros for handling arguments.
- πΌοΈ Type: Interactive Board
- 𧩠Overview: A visual and detailed explanation of variadic functions. Great for those who learn best through diagrams and hands-on exploration.
- Clone the repository:
git clone https://github.com/yomazini/42cursus-ft_printf.git
- Navigate to the project directory:
cd 42cursus-ft_printf
- Compile the library:
# Compile the library
make
# Clean object files
make clean
# Clean everything
make fclean
# Recompile
make re
# Test
make test
- Main Function: ft_printf - The entry point that handles format string parsing
- Format Handler: ft_format_pf - Dispatches to specific type handlers
- Type-Specific Handlers:
- ft_putchar_pf: Character output
- ft_putstr_pf: String output
- ft_putnbr_pf: Integer output
- ft_puthex_pf: Hexadecimal output
- ft_putunbr_pf: Unsigned integer output
- ft_putadr_pf: Pointer address output
- Variadic function handling with
<stdarg.h>
- Recursive printing techniques
- Dynamic type conversion
- Error management
- Memory-efficient character-by-character output
- Handling integer edge cases (e.g., INT_MIN)
- Implementing hexadecimal conversion
- Managing pointer address printing
- Efficient recursive algorithms
- Robust error handling
#include "ft_printf.h"
int main(void) {
int num = 42;
char *str = "Hello, 42!";
ft_printf("Character: %c\n", 'A');
ft_printf("String: %s\n", str);
ft_printf("Decimal: %d\n", num);
ft_printf("Hex (lowercase): %x\n", num);
ft_printf("Hex (uppercase): %X\n", num);
ft_printf("Pointer: %p\n", (void*)&num);
return (0);
}
Recommended testing tools:
- Custom test cases (Go Crazy π)
- printf-tester
To use in your project:
# with main.c File
make test
- Modular design
- Extensive error checking
- Memory safety
- Efficient algorithms
- Clear, readable code
- Minimal memory allocation
- Recursive algorithms with O(log n) complexity
- No unnecessary buffer usage
- Advanced C programming techniques
- Variadic function implementation
- Deep understanding of type conversion
- System-level output handling
Feel free to:
- Open issues
- Submit pull requests
- Provide feedback
Made with βοΈ and βοΈβοΈβοΈ by Youssef Mazini (ymazini)
"In printf we trust, one character at a time!" π