A custom implementation of the printf function for 42 London projects, written in C. This implementation supports various format specifiers and flags, enabling formatted output for strings, characters, integers, unsigned integers, pointers, and hexadecimal values.
- Introduction
- Supported Format Specifiers
- Supported Flags
- Usage
- Function Overview
- Compilation and Testing
- License
The ft_printf function replicates the behavior of the standard printf function, providing formatted output capabilities. This project was created as part of the curriculum at 42 London and demonstrates proficiency in handling variable argument lists, parsing format strings, and applying formatting rules.
The ft_printf function supports the following format specifiers:
| Specifier | Description |
|---|---|
c |
Outputs a single character. |
s |
Outputs a string. |
p |
Outputs a pointer address in hexadecimal format. |
d or i |
Outputs a signed decimal integer. |
u |
Outputs an unsigned decimal integer. |
x |
Outputs an unsigned hexadecimal integer (lowercase). |
X |
Outputs an unsigned hexadecimal integer (uppercase). |
% |
Outputs a literal % character. |
The following flags are supported:
| Flag | Description |
|---|---|
- |
Left-aligns the output within the given width. |
0 |
Pads the output with leading zeros (ignored when - is present). |
. |
Specifies precision for numbers and strings. |
* |
Uses the next argument as the width or precision value. |
[width] |
Specifies a minimum field width. |
%10d— Prints a decimal integer with a minimum width of 10.%05d— Prints a decimal integer padded with leading zeros up to a width of 5.%-10s— Left-aligns a string within a field of width 10.%.3s— Prints only the first 3 characters of a string.%*.*d— Dynamically sets width and precision using arguments from the parameter list.
int ft_printf(const char *format, ...);-
format_print:- Handles different format specifiers (
c,s,p,x, etc.). - Calls helper functions like
ft_string,ft_char,ft_integer, etc., to process specific data types.
- Handles different format specifiers (
-
process_flags:- Parses the format string for flags (
-,0,.,*) and handles width and precision. - Updates the
t_boxstructure to track formatting details for the current conversion.
- Parses the format string for flags (
To compile the ft_printf library:
gcc -Wall -Wextra -Werror ft_printf.c -o ft_printf