ft_printf is a custom implementation of the standard C library function printf. This project aims to replicate the functionality of printf by handling various format specifiers and printing formatted output to the standard output. The project is implemented in C and demonstrates the use of variadic functions to handle a variable number of arguments.
During this project, I learned:
- How to implement variadic functions using
stdarg.h. - The inner workings of the printf function and how format specifiers are processed.
- How to handle different data types dynamically.
- Efficiently converting numbers to different bases (decimal, hexadecimal).
- Manipulating and printing memory addresses (pointers).
- Structuring a C project with multiple source files and a Makefile.
The ft_printf function supports the following format specifiers:
%c: Prints a single character.%s: Prints a string.%dor%i: Prints a signed decimal integer.%u: Prints an unsigned decimal integer.%x: Prints an unsigned hexadecimal integer (lowercase).%X: Prints an unsigned hexadecimal integer (uppercase).%p: Prints a pointer address.
The project consists of the following files:
ft_printf.c- Contains the mainft_printffunction and theprint_formatfunction to handle different format specifiers.ft_printf.h- Header file with function prototypes and necessary includes.ft_strlen.c- Implements theft_strlenfunction to calculate the length of a string.print_digit.c- Contains theprint_digitfunction to print integers in various bases.print_pointer.c- Contains theprint_pointerandpoint_recursivefunctions to print pointer addresses.print_string.c- Contains theprint_stringfunction to print strings.Makefile- Makefile to compile the project and create thelibftprintf.alibrary.
To compile the project, run the following command:
makeThis will generate the libftprintf.a library, which can be linked with your project.
To use the ft_printf function in your program, include the ft_printf.h header and link against the libftprintf.a library.
#include "ft_printf.h"
int main() {
ft_printf("Hello, %s! Your number is %d.\n", "World", 42);
return 0;
}