ft_printf is a custom implementation of the standard C library function printf. This project recreates the behavior of printf with support for the most common format specifiers and flags.
This project was developed as part of the 42 School curriculum to demonstrate understanding of variadic functions, string formatting, and low-level programming concepts in C.
- Character output (
%c) - String output (
%s) - Integer output (
%d,%i) - Unsigned integer output (
%u) - Hexadecimal output (
%x,%X) - Pointer output (
%p) - Percent literal (
%%) - Null string handling
- Return value matching standard printf
| Specifier | Description | Example |
|---|---|---|
%c |
Character | ft_printf("%c", 'A') |
%s |
String | ft_printf("%s", "Hello") |
%d |
Signed decimal integer | ft_printf("%d", -42) |
%i |
Signed integer (same as %d) | ft_printf("%i", 42) |
%u |
Unsigned decimal integer | ft_printf("%u", 12345) |
%x |
Hexadecimal (lowercase) | ft_printf("%x", 255) |
%X |
Hexadecimal (uppercase) | ft_printf("%X", 255) |
%p |
Pointer address | ft_printf("%p", &variable) |
%% |
Percent literal | ft_printf("%%") |
- GCC compiler (or any C compiler)
- Make build system
-
Clone the repository:
git clone <repository-url> cd ft_printf
-
Build the library:
make
This creates
libftprintf.a -
Build and run tests:
make test
-
Include the header:
#include "ft_printf.h"
-
Link with the library:
gcc your_program.c -L. -lftprintf -o your_program
-
Use in your code:
#include "ft_printf.h" int main(void) { ft_printf("Hello, %s! The answer is %d\\n", "world", 42); return (0); }
The project includes comprehensive tests to verify functionality against the standard printf. Run tests with:
make testThis compares output and return values between the custom implementation and the standard library function.
ft_printf/
├── ft_printf.c # Main printf function implementation
├── ft_printf.h # Header file with function declarations
├── utils.c # Utility functions (strlen, putchar, putstr)
├── ft_putnbr.c # Number and base conversion functions
├── test_ft_printf.c # Test program
├── Makefile # Build configuration
└── README.md # This file
ft_printf(): Main function that processes format strings and argumentsft_putnbr(): Handles signed integer outputft_putnbr_base(): Handles base conversions (decimal, hex, etc.)ft_pointr(): Handles pointer address output- Utility functions for character and string output
- Variadic function handling using
va_list,va_start,va_arg - Recursive number conversion for efficient digit processing
- Base conversion support for multiple number formats
- Proper return value counting matching standard printf behavior
- No dynamic memory allocation
- Uses static buffers for number conversion
- Efficient character-by-character output
- Handles null strings gracefully
- Proper handling of negative numbers
- Robust base conversion with validation
- Optimized for 42 School coding standards
- Minimal function call overhead
- Efficient string and number processing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
Miguel González Clayton
- GitHub: migclay12
- 42 School: Madrid
This project demonstrates fundamental C programming concepts including variadic functions, string manipulation, and low-level I/O operations.