This project has been created as part of the 42 curriculum by csekakul.
Welcome to ft_printf, a custom structural implementation of the standard printf function found in libc. This project marks the transition from using static output utilities to engineering dynamic formatting pipelines using C's native variadic argument interfaces.
The core objective of this project is to replicate the format-parsing string mechanics of printf without relying on standard buffer-pooling frameworks. The function evaluates characters sequentially, routes specifiers through an internal parsing core, dynamically handles base conversions via custom memory allocation subroutines, tracks the total printed-byte length via an internal counter, and interfaces safely with low-level kernel write streams.
π‘ Scope Limitation: This implementation satisfies the mandatory criteria. It does not process field minimum widths or complex attribute formatting flags (
-,0,.,#,+, or spaces).
- Language: C (Strictly compliant with The Norm standard)
- Compilation Tooling:
ccutilizing diagnostic flags-Wall -Wextra -Werror - Native Context Requirements: Integrated tracking types and system resources:
<stdarg.h>β For variadic argument management (va_list,va_start,va_arg,va_end)<unistd.h>β For system stream access (write)<stdlib.h>β For isolated byte allocations (malloc,free)<stdint.h>β For macro evaluation limits (SIZE_MAX)
π ft_printf/
βββ π ft_printf.h # Central Header (Macros, Types, System Inclusions)
βββ π ft_printf.c # Primary Entry Point & Character Iteration Loop
βββ π ft_putchar_pf.c # Safe Single Character Output Subroutine
βββ π ft_putstr_pf.c # String Output Handler with Native Edge Safety
βββ π ft_putnbr_pf.c # Base-10 Signed Integer Translation Core
βββ π ft_putuint_pf.c # Base-10 Unsigned Integer Format Handler
βββ π ft_puthex_pf.c # Low-Level Base-16 Hexadecimal Engine
βββ π ft_putptr_pf.c # Unsigned Address Conversion & Pointer Printer
βββ π ft_aux_pf.c # Explicit Memory Allocations & Parsing Utils
βββ π Makefile # Multi-File Archiver Lifecycle Controller
βββ π README.md # Structural Overview & Documentation Matrix
| Format | Behaviour |
|---|---|
| %c | Prints a single character |
| %s | Prints a string (as defined by the common C convention) |
| %p | The void * pointer argument has to be printed in hexadecimal format |
| %d | Prints a decimal (base 10) number |
| %i | Prints an integer in base 10 |
| %u | Prints an unsigned decimal (base 10) number |
| %x | Prints a number in hexadecimal (base 16) lowercase format |
| %X | Prints a number in hexadecimal (base 16) uppercase format |
| %% | Prints a percent sign |
Printf handles variable number of arguments. It is necessary because we don't know in advance how many arguments is the user going to ask to print. The ... stands for the variable arguments in the function prototype. Then you need to initiate it as if it was a normal variable va_list va;. Before starting to use it in the function you need to start it with va_start(va, str); and end it at the end it of the function with va_end(va);.
- Gitbook ft_printf guide
- Makefile tutorial
- C notes, debugging
- man printf
- man va_list
- man va_arg
For this project I used AI for the compilation, because I was unsure how am I supposed to compile a complex project with multiple files, and create one executable file. I learned it with the help of my peers and asking AI to explain too.
In my ft_printf.h file I am including the following libraries:
- stdarg.h
- It is necessary for the use of the variadic functions.
- unistd.h
- It is necessary to use the function write.
- stdlib.h
- It is necessary, because I am using malloc.
- stdint.h
- It is necessary, because I am using SIZE_MAX.
| Action | Command |
|---|---|
| You can compile it with | make |
| Delete object files | make clean |
| Delete all objects | make fclean |
| Delete and recompile from scratch | make re |
-
First create the
main.cfile. -
Now run
maketo create the object files. -
Then create the executable file with
cc -Wall -Wextra -Werror main.c libftprintf.a. -
Finally you can simply execute the program with
./a.out.
#include "ft_printf.h"
#include <stdio.h>
int main(void)
{
ft_printf("Hello %s! Number: %d\n", "42", 42);
ft_printf("Hex: %x | Ptr: %p\n", 255, &main);
return (0);
}