https://github.com/nabilac27/42-ft_printf
an implementation of printf function, utilizing variadic functions to handle formatted output with custom parsing and formatting logic.
https://github.com/nabilac27/42-ft_printf
dynamic-memory-allocation error-handling memory-management string-formatting string-manipulation variadic-function
Last synced: 9 months ago
JSON representation
an implementation of printf function, utilizing variadic functions to handle formatted output with custom parsing and formatting logic.
- Host: GitHub
- URL: https://github.com/nabilac27/42-ft_printf
- Owner: nabilac27
- Created: 2024-11-10T18:52:02.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-10T02:22:05.000Z (10 months ago)
- Last Synced: 2025-04-10T03:27:52.779Z (10 months ago)
- Topics: dynamic-memory-allocation, error-handling, memory-management, string-formatting, string-manipulation, variadic-function
- Language: C
- Homepage:
- Size: 1.49 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: docs/README.md
Awesome Lists containing this project
README
# **ft_printf**
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**.
## **About ft_printf**
`ft_printf` is 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.
## **Key Concepts**
- **Variadic Functions**:
- The `` library is used to handle variadic functions, enabling the processing of a variable number of arguments.
- Key macros:
- `va_start`: Initializes the `va_list` for use.
- `va_arg`: Retrieves the next argument from the list.
- `va_end`: Cleans up the `va_list` after processing.
- **Return Type**:
- The function returns the total number of characters printed.
- If an error occurs, `ft_printf` returns `-1`.
---
## **Functions Overview**
| **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. |