This project is a partial reimplementation of the C standard library function printf. It's a core project in the 42 school curriculum, designed to deepen understanding of C programming, especially variadic functions.
The ft_printf function mimics the behavior of the original printf. It takes a format string and a variable number of arguments, then writes the formatted output to the standard output.
This implementation is based on the included libft library, which contains a set of custom, re-coded standard C library functions.
The following conversion specifiers are supported:
%c: Prints a single character.%s: Prints a string of characters.%p: Prints a pointer address 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 literal percent sign.
To compile the project and create the static library (libftprintf.a), follow these steps:
- Clone the repository:
git clone https://github.com/your-username/ft_printf.git
- Navigate to the project directory:
cd ft_printf - Run the Makefile:
This will compile all the necessary source files and create the
make
libftprintf.alibrary file.
To use the ft_printf function in your own project:
- Include the header file
ft_printf.hin your C source files. - When compiling your project, link the
libftprintf.alibrary.
Example main.c:
#include "ft_printf.h"
int main(void)
{
char *str = "world";
int num = 42;
void *ptr = #
ft_printf("Hello, %s!\n", str);
ft_printf("The magic number is %d (or %i).\n", num, num);
ft_printf("Its unsigned value is %u.\n", num);
ft_printf("In hexadecimal, it's %x (lowercase) or %X (uppercase).\n", num, num);
ft_printf("The address of the number is: %p\n", ptr);
ft_printf("This is a test of the percent sign: %%%%\n");
return (0);
}Compilation command:
cc main.c -L. -lftprintf -o my_programThis command links your main.c with the libftprintf.a library (the -L. tells the compiler to look in the current directory for libraries, and -lftprintf links the library).
The provided Makefile includes the following rules:
make allormake: Compiles theft_printflibrary andlibft.make clean: Removes all object files (.o).make fclean: Removes all object files and the finallibftprintf.alibrary file.make re: Runsfcleanand thenallto recompile the library from scratch.