Welcome to ft_printf, a project developed as part of the 42 curriculum. This is a handcrafted implementation of the standard printf function in C, designed to deepen understanding of variadic functions, format specifiers, and low-level output formatting.
β Handles standard format specifiers:
- %c β Character
- %s β String
- %d / %i β Integer
- %u β Unsigned Integer
- %p β Pointer Address
- %x / %X β Hexadecimal (lower/uppercase)
- %% β Print a literal
%
β Bonus Implementation: Supports essential formatting flags:
- - β Left-align output
- 0 β Zero-padding
- # β Prefix for hexadecimal (
0x,0X) - + β Force display of sign for numeric values
- (space) β Add a space before positive numbers
- .precision β Control string truncation & numeric precision
- β Does not support floating-point numbers (
%f,%e,%g). - β Does not support octal (
%o).
ft_printf
βββ includes/ # Header files
βββ [libft](https://github.com/LordMikkel/libft)/ # Custom string & memory functions
βββ src/ # Core implementation of ft_printf
β βββ ft_printf.c # Main function
β βββ utils/ # Helper functions for formatting
βββ src_bonus/ # Extended flags functionality
β βββ ft_printf_bonus.c
β βββ utils/ # Bonus-specific utilities
βββ Makefile # Build automation
βββ README.md # You're reading it!
git clone git@github.com:LordMikkel/ft_printf.git
cd ft_printfmakeThis will generate libftprintf.a, a static library that can be linked to your project.
#include "ft_printf.h"
int main()
{
ft_printf("Hello, %s! You have %d new messages.\n", "Mikel", 42);
return 0;
}The Makefile includes a set of rules that help streamline the project build process and testing:
Compiles the project and generates the libftprintf.a static library. This is the foundation for your ft_printf implementation.
Compiles and links the bonus features of the project, enabling additional formatting options such as custom flags (e.g., Min_width, -, 0, #, +, etc.). This produces a version of the library with these extended functionalities.
Compiles and runs a comprehensive suite of tests for basic and complex formats. The test suite ensures your ft_printf works as expected, comparing it against the standard printf implementation. Tests cover characters, strings, integers, hexadecimals, and combinations.
Compiles and runs tests specifically designed for the bonus features. These tests verify that flags such as Min_width, -, 0, #, +, and space are handled correctly. Bonus functionality ensures your ft_printf is more flexible and robust.
Removes all object files (.o) from the src/ and src_bonus/ directories, cleaning up intermediate build files to prepare for fresh compilations.
Removes both object files and the compiled static library (libftprintf.a). This ensures the project is entirely cleaned, removing all generated files.
Performs a complete rebuild of the project from scratch by running make fclean followed by make. This is perfect when you need to ensure that all files are freshly compiled.
In my main.c, several tests are executed to check the correctness of the ft_printf implementation. The tests encompass a variety of formats and edge cases, ensuring comprehensive coverage. Here's an overview:
- Characters: Test for basic characters like
'A', null characters (\0), and special characters like newline (\n). - Strings: Tests for normal strings, empty strings, and long strings to check memory handling and performance.
- Tests for printing valid pointer addresses and handling
NULLpointers.
- Integers: Tests for positive and negative integers, as well as boundary values like
INT_MAXandINT_MIN. - Unsigned Integers: Tests for unsigned integers, including edge cases like
UINT_MAX.
- Verifying the correct representation of hexadecimal numbers in both lowercase (
%x) and uppercase (%X).
- Tests involving multiple format specifiers in a single print statement, ensuring that combinations work as expected.
- Tests for additional flags such as
Min_width,-,0,#,+, and space. These flags offer more control over the printed output. - Tests with combinations of flags ensure that they work correctly when used together, e.g.,
%-10.5d,%010d, and%#x.
Each test compares the output of ft_printf with the standard printf output. If the outputs match, the test passes; otherwise, it fails. At the end of the test suite, a summary will display:
- Passed Tests: Displayed in green π©.
- Failed Tests: Displayed in red π₯.
This printf implementation passes all the tests from ultimate ft_printf_test when you execute the 938 tests, your terminal may display an overwhelming number of warningsβdonβt panic! Just let it run, and in the end, youβll see the final test results.
Developed by Mikel Garrido as part of the 42 curriculum.
I want to thank madebypixel02 for the super cool progress bars, cacharle for the ultimate test, and vdaniely for the initial main
π GitHub: LordMikkel


