A custom C implementation of the `printf` standard function.
Master variadic arguments, format string parsing, and output buffering. Seamlessly processes standard input and output streams in C.
"The screen is a canvas, and standard output is the brush. But how does the machine know what to paint when the instructions are variable?"
Welcome to ft_printf, the dispatcher.
While standard libft functions deal with fixed parameters, this project introduces the chaos of the unknown: Variadic Arguments.
The goal was to recode the standard C library printf function. This required building a robust parsing engine capable of reading a string, identifying format specifiers (%), and dynamically fetching the correct data type from memory at runtime using the stdarg.h macros.
This is not just a printing tool; it's a study in State Machines, Memory Extraction, and Base Conversions.
The development of this engine is divided into two distinct engineering phases:
- The Core Dispatcher (Mandatory): Replicating the fundamental behavior of
printffor standard data types and base conversions. - The Flag Management Engine (Bonus): Upgrading the parser into a strict State Machine capable of handling complex layout sequences, minimum field widths, and precision padding (
-0. # +).
Click to decrypt the logic behind each build:
🔹 Module I: The Core Dispatcher ( Mandatory )
The Base Protocol: Handling the 9 fundamental conversion specifiers required to replicate standard
printfbehavior.
The engine parses the format string and dynamically extracts arguments using va_start, va_arg, and va_end.
📂 View Specifier Catalog
| Specifier | Technical Description | Expected Data Type |
|---|---|---|
%c |
Prints a single ASCII character. | int (promoted from char) |
%s |
Prints a null-terminated string. | char * |
%p |
Prints a memory address (pointer) in hexadecimal format. | void * |
%d |
Prints a signed decimal (base 10) integer. | int |
%i |
Prints a signed integer (same behavior as %d in this context). |
int |
%u |
Prints an unsigned decimal (base 10) integer. | unsigned int |
%x |
Prints an unsigned number in lowercase hexadecimal (base 16). | unsigned int |
%X |
Prints an unsigned number in uppercase hexadecimal (base 16). | unsigned int |
%% |
Prints a literal percent sign. | None |
🔸 Module II: The Flag Management Engine ( Bonus ) — 🚧 [ IN PROGRESS ]
The Bonus module is currently under active development. While the Core Dispatcher handles raw data types, this expansion upgrades the parser into a strict State Machine capable of handling complex layout sequences.
Current Engineering Focus:
- 🛠️ Struct Latching: Building the control structure to store detected flags (
-0. # +) before the final conversion specifier is reached. - 📏 Field Width & Precision: Calculating dynamic padding spaces and zero-fills accurately.
- 🧠 Edge-Case Routing: Ensuring the engine doesn't break when multiple contradictory flags are combined in a single string.
The full implementation and flag routing tables will be deployed in a future commit.
The project is built with a simple and modular architecture. It strictly separates the string parsing logic from the actual data conversion, keeping the codebase clean and easy to maintain.
The engine follows a straightforward execution loop:
- Parse: Reads the format string character by character.
- Detect: When a
%is found, it evaluates the next character to identify the specifier. - Dispatch: Calls the specific rendering function (e.g., string, hex, integer) and extracts the argument using
va_arg. - Output: Prints the formatted data to the standard output and keeps a running total of the printed length.
To build the library, ensure you have a C compiler (gcc or clang) and make installed.
Clone the repository:
git clone [https://github.com/joolibar/ft_printf.git](https://github.com/joolibar/ft_printf.git)
cd ft_printfCompile the library:
makeOnce compiled, a libftprintf.a archive is generated. Include the header in your .c files and link the static library during compilation:
#include "ft_printf.h"
int main(void)
{
ft_printf("System boot: %s\n", "OK");
ft_printf("Memory address: %p\n", &main);
return (0);
}Compile your project with the library:
gcc -Wall -Wextra -Werror your_file.c -L. -lftprintf -o your_program"Raw data is just memory. Formatted output is how the machine finds its voice." 🗣️⚙️
Found this library useful?
⭐ Drop a star
|
👀 Follow my journey
Crafted by joolibar
Creative Developer building digital experiences at 42
© 2024 joolibar • Validated by Moulinette 🤖, crafted by Humans 🧠.