This project has been created as part of the 42 curriculum by spopa.
This project is a reimplementation of the standard C library function printf. It handles the basic cspdiuxX% conversions, mimicking the behaviour of the original.
| Specifier | Description |
|---|---|
%c |
Prints a single character. |
%s |
Prints a string (as defined by the common C convention). |
%p |
The void * pointer argument has to be printed in hexadecimal format. |
%d |
Prints a decimal (base 10) number. |
%i |
Prints an integer in base 10. |
%u |
Prints an unsigned decimal (base 10) number. |
%x |
Prints a number in hexadecimal (base 16) lowercase format. |
%X |
Prints a number in hexadecimal (base 16) uppercase format. |
%% |
Prints a percent sign. |
- Clone the repository.
- Run
maketo compile the librarylibftprintf.a. - Include the header
ft_printf.hin your project and link the library.
#include "ft_printf.h"
int main() {
int len = ft_printf("Hello %s, number %d\n", "World", 42);
ft_printf("Printed %d characters\n", len);
return (0);
}- Algorithm: The function iterates linearly through the format string. Standard characters are written immediately to the output. When a
%is encountered, a dispatcher function identifies the flag and calls the specific printing function usingva_arg. - Data Structure:
va_list(from<stdarg.h>) is used to manage the variable number of arguments. - Recursion: To print numbers (
%d,%x, etc.) without buffer management (as forbidden by the subject), a recursive approach is used. This allows printing digits in the correct order directly to the standard output without memory allocation.
- To study how the variadic functions behaved i used this YT video from the channel CodeVault.
man va_(start, arg, end)- This tester made by Tripouille.