This repository contains my implementation of the ft_printf function, a core project in the 42 School curriculum.
The goal of this project is to recreate the standard C printf function
while meeting specific requirements and constraints,
deepening the understanding of variadic functions, formatted output,
and memory management.
ft_printfis a variadic function, meaning it accepts a variable number of arguments.- The first argument is a format string that defines how the subsequent arguments are formatted and displayed.
- The function processes the format string, parsing specifiers like
%d,%s,%c, etc., and outputs the corresponding formatted values.
-
Variadic Functions:
- The
<stdarg.h>library is used to handle variadic functions, enabling the processing of a variable number of arguments. - Key macros:
va_start: Initializes theva_listfor use.va_arg: Retrieves the next argument from the list.va_end: Cleans up theva_listafter processing.
- The
-
Return Type:
- The function returns the total number of characters printed.
- If an error occurs,
ft_printfreturns-1.
ft_printf/
|
├── Makefile
|
├── ft_printf.h
|
├── ft_printf.c
|
├── ft_printf_digits.c
|
├── ft_printf_hex.c
|
├── ft_printf_words.c
|
└── main.c| Function | Description |
|---|---|
ft_printf(const char *str, ...) |
The main function that processes the format string and handles the variadic arguments. It returns the total number of characters printed. |
ft_printf_format(char specifier, va_list arg_ptr) |
A helper function that processes individual format specifiers (e.g., %c, %d, %s). |
ft_printf_c(int c) |
Handles the %c format specifier and prints a character. |
ft_printf_s(char *s) |
Handles the %s format specifier and prints a string. |
ft_printf_d(int n) |
Handles the %d format specifier and prints a signed integer. |
ft_printf_u(unsigned int n) |
Handles the %u format specifier and prints an unsigned integer. |
ft_printf_p(void *ptr) |
Handles the %p format specifier and prints a pointer address. |
ft_printf_x(unsigned int n, char specifier) |
Handles the %x and %X format specifiers to print a hexadecimal value (lowercase/uppercase). |
ft_printf_hex(unsigned long n) |
Prints a hexadecimal value (lowercase) of the given unsigned long integer. |
ft_printf_hex_upper(unsigned long n) |
Prints a hexadecimal value (uppercase) of the given unsigned long integer. |
- Clone the repository
git clone https://github.com/your-username/ft_printf.git
cd ft_printf- Compile the library
makeThis will generate the static library:
libftprintf.a
Include the header in your C file:
#include "ft_printf.h"Compile your program with the library:
cc main.c libftprintf.aThen run:
./a.outExample program using ft_printf:
#include "ft_printf.h"
int main(void)
{
int len;
len = ft_printf("Hello %s!\n", "World");
ft_printf("Printed %d characters\n", len);
ft_printf("Character: %c\n", 'A');
ft_printf("Decimal: %d\n", 42);
ft_printf("Unsigned: %u\n", 42);
ft_printf("Hex lowercase: %x\n", 255);
ft_printf("Hex uppercase: %X\n", 255);
ft_printf("Pointer: %p\n", &len);
return (0);
}Hello World!
Printed 13 characters
Character: A
Decimal: 42
Unsigned: 42
Hex lowercase: ff
Hex uppercase: FF
Pointer: 0x7ffee3b6c9ac