A recreation of the C standard library printf function with support for various format specifiers.
ft_printf is a project that involves recreating the famous printf function from the C standard library. This project teaches you about variadic functions, format parsing, and proper output formatting. It handles multiple data types and conversion specifiers.
- Supports major format specifiers:
%c,%s,%p,%d,%i,%u,%x,%X,%% - Handles variadic arguments
- Proper conversion and formatting
- Memory-safe implementation
- Compatible with the original printf behavior
int ft_printf(const char *format, ...);%c: Character%s: String%p: Pointer address (hexadecimal)%d: Signed decimal integer%i: Signed integer%u: Unsigned decimal integer%x: Hexadecimal integer (lowercase)%X: Hexadecimal integer (uppercase)%%: Literal % character
makeThis creates a libftprintf.a static library.
- Clone the repository:
git clone https://github.com/bratzwitch/ft_printf.git
cd ft_printf- Compile the library:
make- Include in your project:
#include "ft_printf.h"- Compile your program:
gcc -Wall -Wextra -Werror your_file.c -L. -lftprintf#include "ft_printf.h"
int main()
{
int num = 42;
char *str = "Hello";
void *ptr = #
ft_printf("Character: %c\n", 'A');
ft_printf("String: %s\n", str);
ft_printf("Pointer: %p\n", ptr);
ft_printf("Decimal: %d\n", num);
ft_printf("Hexadecimal: %x\n", num);
ft_printf("Uppercase hex: %X\n", num);
ft_printf("Unsigned: %u\n", 3000000000U);
ft_printf("Percentage: %%\n");
return (0);
}Returns the number of characters printed (excluding the null terminator).
ft_printf.c: Main function implementationft_printf_utils.c: Helper functions for conversionsft_printf.h: Header file with prototypesMakefile: Compilation rules
- Uses
write()system call for output - Handles edge cases (NULL pointers, zero values)
- Proper memory management
- No global variables used
- Follows 42 Norm coding standards
- GCC compiler
- Make
- Standard C library headers
#include "ft_printf.h"
#include <stdio.h>
int main()
{
int ret1, ret2;
ret1 = ft_printf("ft_printf: %d %s %x\n", 42, "test", 255);
ret2 = printf("printf: %d %s %x\n", 42, "test", 255);
printf("ft_printf returned: %d\n", ret1);
printf("printf returned: %d\n", ret2);
return (0);
}Viacheslav Moroz - 42 Student