A custom implementation of the printf function - 42 School Project
ft_printf is a 42 School project that involves recoding the standard C library function printf(). This project teaches about variadic functions, parsing, and how one of the most used functions in C actually works under the hood.
The goal is to create a library containing a function ft_printf() that mimics the behavior of the original printf function from the C standard library. The function handles multiple format specifiers and returns the number of characters printed.
The ft_printf function handles the following format specifiers:
- %c - Print a single character
- %s - Print a string
- %p - Print a pointer address in hexadecimal format
- %d - Print a signed decimal integer
- %i - Print a signed integer
- %u - Print an unsigned decimal integer
- %x - Print a number in lowercase hexadecimal format
- %X - Print a number in uppercase hexadecimal format
- %% - Print a percent sign
ft_printf()- Main function that formats and prints outputft_exec_flag()- Parses and executes format specifiersft_putchar()- Outputs a single characterft_putstr()- Outputs a stringft_putnbr()- Outputs a signed integerft_putunbr()- Outputs an unsigned integerft_print_in_hexa_min()- Outputs lowercase hexadecimalft_print_in_hexa_max()- Outputs uppercase hexadecimalft_print_void_p()- Outputs pointer address
- GCC compiler
- Make
To compile the library:
makeThis will create the static library libftprintf.a.
Remove object files:
make cleanRemove object files and the library:
make fcleanRecompile everything:
make re- Compile the library:
make- Include the header in your C file:
#include "ft_printf.h"- Compile your program with the library:
gcc your_program.c libftprintf.a -o your_program#include "ft_printf.h"
int main(void)
{
int num = 42;
char *str = "Hello, World!";
void *ptr = #
// Print different types
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("Integer: %i\n", -42);
ft_printf("Unsigned: %u\n", 42);
ft_printf("Hex (lower): %x\n", 255);
ft_printf("Hex (upper): %X\n", 255);
ft_printf("Percent: %%\n");
// Returns number of characters printed
int ret = ft_printf("Test: %d\n", 123);
ft_printf("Characters printed: %d\n", ret);
return (0);
}Character: A
String: Hello, World!
Pointer: 0x7ffd5c4a3b8c
Decimal: 42
Integer: -42
Unsigned: 42
Hex (lower): ff
Hex (upper): FF
Percent: %
Test: 123
Characters printed: 9
ft_printf/
├── Makefile # Build configuration
├── include/
│ └── ft_printf.h # Header file with function prototypes
├── src/
│ ├── ft_printf.c # Main ft_printf implementation
│ ├── prints_one.c # Helper functions for printing (part 1)
│ └── prints_two.c # Helper functions for printing (part 2)
└── README.md # This file
- Language: C
- Compiler: GCC
- Compilation Flags:
-Wall -Wextra -Werror - Library Type: Static library (
.a) - Key Concepts:
- Variadic functions (
va_list,va_start,va_arg,va_end) - Format string parsing
- Type conversions
- Hexadecimal representation
- Return value management
- Variadic functions (
- Norm: 42 School coding standard (Norminette)
The project uses the <stdarg.h> library to handle variable argument lists:
va_list- Type for argument listva_start()- Initialize argument listva_arg()- Retrieve next argumentva_end()- Clean up argument list
Like the original printf, ft_printf returns the total number of characters printed to the standard output.
The implementation is split across multiple files for better organization:
- Core printf logic in
ft_printf.c - Print helper functions in
prints_one.candprints_two.c - Function prototypes in
ft_printf.h
This project is part of the 42 School curriculum, focusing on understanding variadic functions and reimplementing standard library functions.
bmetehri - 42 Student
Project completed: January 2023