This project has been created as part of the 42 curriculum by iuslu.
ft_printf is a project that involves recoding the standard C library's printf function. The primary goal of this project is to understand the mechanics of variadic functions in C and to build an efficient data formatting system. The project supports the following conversion specifiers: %c, %s, %p, %d, %i, %u, %x, %X, and %%.
The project is compiled using the Makefile located in the root directory. You can use the following commands:
make: Compiles the source files and creates thelibftprintf.alibrary.make clean: Removes the object (.o) files.make fclean: Removes object files and the generated library file.make re: Cleans and recompiles the entire project.
To include the libftprintf.a library in your own project:
- Include the
ft_printf.hheader file in your code. - Link the library during compilation:
cc main.c libftprintf.a -o my_program
- Man Pages:
man 3 printf,man 3 stdarg. - Documentation: C Variadic Functions (gnu.org).
- Reference: Standard C Library documentation regarding
writeandva_list.
In this project, Artificial Intelligence (Gemini) was utilized for the following tasks:
- Debugging: Identifying and resolving memory access issues (potential buffer overflows) and handling
NULLpointer cases (specifically the(nil)output for%p). - Documentation: Structuring this README file and refining technical explanations according to 42 requirements.
The project is built on an algorithm that parses the format string in a single pass.
- Traversal: The format string is scanned character by character using a
whileloop. - Detection: When a
%character is encountered, the next character is checked to see if it is a valid conversion specifier. - Dispatching: Based on the specifier, the corresponding data type is fetched from the stack using the
va_argmacro. - Recursive Conversion: Recursive functions are used for hexadecimal and integer conversions. This allows digits to be printed directly to the standard output without the need for a temporary buffer, ensuring memory efficiency.
- Counter: Every successful
writeoperation is added to thecheckcounter, which serves as the function's total return value.