English | Español
This project has been created as a part of the 42 curriculum by mechicai
In this project, I recreate the basic behavior of the printf() function from the C standard library (libc) The function must be able to print formatted text using only the 'write' function The following format specifiers are implemented:
- %c -> a character
- %s -> a string
- %p -> the void * pointer (printed in hexadecimal format)
- %d -> a decimal number (base 10)
- %i -> an integer (base 10)
- %u -> an unsigned decimal number (base 10)
- %x -> a lowercase hexadecimal number (base 16)
- %X -> an uppercase hexadecimal number (base 16)
- %% -> the percent symbol (%)
- To compile the library -> make
- This generates the static library libftprintf.a from the .c files listed in the Makefile
- To clean intermediate files and rebuild the library:
- make clean -> removes the .o files
- make fclean -> removes the .o files and libftprintf.a
- make re -> fclean + all
- To test the library with a sample program:
- Create a .c file with a main. Example: test.c
- Compile the program by linking the library locally with the following command: cc -I. test.c libftprintf.a -o test
- Run the program: ./test
- man printf
- man write
- Guide used as reference: https://42-cursus.gitbook.io/guide/1-rank-01/ft_printf
- I used AI to understand better the following theoretical concepts:
- Variadic functions
- The <stdarg.h> library and what va_list, va_start, va_arg, va_end do
I used:
- Recursion to print numbers (in base 10 and base 16)
I used recursion to break a number down into its digits because:
- It provides a straightforward way to print digits from left to right without intermediate buffers (this complies with the project requirement that forbids handling buffers like the original printf implementation)
- Hexadecimal conversion using a character table
- I use the string "123456789abcdef" (and its uppercase version) as a lookup table to convert values [0..15] into their corresponding characters (used to print numbers in base 16: %x, %X, and %p)
- write() for standard output
- The project specification states that "the management of the original printf buffer must not be implemented," meaning I cannot use functions that perform buffering (temporary storage of data in memory before sending it to its final destination). Therefore, I use write to print directly to standard output without any internal buffering
- Use of <stdarg.h>
- This is the C standard library that allows functions to accept a variable number of arguments. Thanks to it, I can access the variable arguments passed to my ft_printf function
- ft_printf uses va_list, va_start, va_arg, and va_end to read arguments at runtime, dynamically deciding what to print depending on the format specifier (%c, %s, %d, etc.)
The implementation is split into small functions (ft_write_char.c, etc.) to make unit testing easier, improve readability, and encourage code reuse
The project generates a static library called libftprintf.a that contains all the functions of ft_printf. This library can be linked from a main.c file or any other C source file, making it easy to compile test programs while avoiding external dependencies at runtime. This is because a static library is embedded into the executable during compilation, so the program does not rely on external files being installed when it runs
This implementation reproduces only the mandatory format specifiers required by the project and does not implement flags, width, precision, or advanced formatting options