ft_printf
A comprehensive implementation of the C standard library printf function. This project’s aim is to take the very simple write command and, using only that, implement the complex features of printf.
Capabilities
Conversions
| Specifier | Description |
|---|---|
%c | Single character |
%s | String of characters |
%p | Pointer address (hex) |
%d / %i | Decimal (base 10) number |
%u | Unsigned decimal number |
%x | Hexadecimal number (lowercase) |
%X | Hexadecimal number (uppercase) |
%% | Percent sign |
Flags & Options
| Flag | Description |
|---|---|
- | Left-align the result within the given field width. |
0 | Left-pads numbers with zeros instead of spaces. |
. | Precision: minimum digits for integers, max characters for strings. |
# | Alternate form: adds 0x or 0X prefix for hex values. |
space | Prepends a blank space before positive numbers. |
+ | Forces a sign (+ or -) to be displayed for signed numbers. |
| [number] | Sets the minimum field width for the output. |
Examples
Here is how ft_printf handles complex flag combinations:
Width & Precision
Managing minimum width and specific precision simultaneously.
;
// Output: "Result: | 00042|"
// Width 10, Precision 5 (pads 0s to 5 digits, then spaces to 10 width).
Left Alignment & Padding
Using the minus flag to flip alignment.
;
;
// Output:
// Standard: | 42|
// Left-Adj: |42 |
Explicit Signs
Forcing signs on positive numbers.
;
;
// Output:
// Signed: +42
// Space: 42
Implementation Details
This project uses a modular “Tokenize & Process” architecture:
- Parsing: ft_split_parts separates the format string into literal segments and conversion tokens.
- Dispatch: ft_eval_arg routes specific types to their handlers.
- Construction: ft_merge_prints calculates total length based on:
- Left Padding (Width/Flags)
- Core Content (Precision/Value)
- Right Padding (Alignment)
Build & Run
Compile the library:
Use in your code:
int