Skip to content

Repository files navigation

🖨️ ft_printf

Language Focus Concepts

A reimplementation of the standard C printf() function written from scratch, designed to demonstrate variadic functions, formatted output processing, and low-level memory handling.

ft_printf is one of the fundamental projects of the 42 curriculum, where the goal is to recreate the behavior of the standard printf function using only a limited subset of the C standard library.


Quick Overview

  • Built with: C
  • Focus: Recreating printf-like output formatting, variadic functions, and format parsing.
  • What I learned: Modular design, parsing logic, variadic arguments, type handling, and careful output management.

📌 Project Objective

Recreate the function:

int ft_printf(const char *format, ...);

The function must:

  • Parse a format string

  • Detect format specifiers

  • Retrieve variadic arguments

  • Format and print the output correctly

  • Return the number of printed characters


⚙️ Supported Format Specifiers

Specifier Description
%c Print a character
%s Print a string
%p Print a pointer address
%d Print a decimal integer
%i Print an integer
%u Print an unsigned integer
%x Print hexadecimal (lowercase)
%X Print hexadecimal (uppercase)
%% Print a percent sign

🧠 Core Concept — Variadic Functions

ft_printf relies on variadic arguments, allowing functions to accept a variable number of parameters.

Example:

int ft_printf(const char *format, ...);

Arguments are accessed using:

va_list args;
va_start(args, format);
va_arg(args, type);
va_end(args);

Flow:

ft_printf()
   │
   ▼
parse format string
   │
   ▼
detect %
   │
   ▼
retrieve argument
   │
   ▼
format output
   │
   ▼
write to stdout

🔎 Parsing the Format String

The algorithm scans the string character by character.

Example:

ft_printf("Hello %s %d", name, age);

Parsing Steps

H → print
e → print
l → print
l → print
o → print
(space)
% → specifier detected
s → print string
(space)
% → specifier detected
d → print integer

🧩 Conversion Handling

Each specifier triggers a dedicated conversion function.

Example architecture:

ft_printf
 │
 ├── handle_char
 ├── handle_string
 ├── handle_pointer
 ├── handle_integer
 ├── handle_unsigned
 ├── handle_hex
 └── handle_percent

Each handler:

  • Retrieves the argument using va_arg

  • Converts it to a printable format

  • Writes it using write()

📊 Output Flow

FORMAT STRING
      │
      ▼
  ft_printf
      │
      ▼
FORMAT PARSER
      │
      ▼
SPECIFIER DETECTED
      │
      ▼
ARGUMENT RETRIEVAL
      │
      ▼
CONVERSION FUNCTION
      │
      ▼
write() → STDOUT

📁 Project Structure

ft_printf/
│
├── ft_printf.c
├── ft_printf.h
│
├── ft_putchar.c
├── ft_putstr.c
├── ft_putnbr.c
│
├── ft_puthex.c
├── ft_putptr.c
│
└── README.md
Core Files File Role
ft_printf.c Main formatting engine
ft_printf.h Function prototypes
ft_putchar.c Character printing
ft_putstr.c String printing
ft_putnbr.c Integer printing
ft_puthex.c Hexadecimal conversion
ft_putptr.c Pointer printing

▶️ Example Usage

#include "ft_printf.h"

int main(void)
{
    ft_printf("Hello %s\n", "world");
    ft_printf("Number: %d\n", 42);
    ft_printf("Hex: %x\n", 255);
    ft_printf("Pointer: %p\n", &main);
}

📄 Example Output

Hello world
Number: 42
Hex: ff
Pointer: 0x7ffee4b6c9a0

⚡ Compilation

Compile using:

cc -Wall -Wextra -Werror *.c

Or with a Makefile:

make

🧩 Challenges Solved

  • Implementing printf from scratch requires solving several non-trivial problems:

  • Parsing formatted strings

  • Handling variadic arguments

  • Implementing number-to-string conversions

  • Managing hexadecimal and pointer formatting

  • Handling edge cases

  • Returning accurate printed length


🛠 Skills Developed

  • Low-Level Programming

  • variadic functions

  • formatted output

  • system calls (write)

  • Memory Handling

  • stack argument management

  • integer conversion algorithms

  • Algorithm Design

  • parsing engines

  • format interpretation

  • Debugging

  • segmentation faults

  • incorrect format parsing

  • integer conversion bugs


📚 Concepts Mastered

  • variadic functions (stdarg.h)

  • formatted string parsing

  • integer-to-string conversion

  • hexadecimal representation

  • pointer formatting


👨‍💻 Author

Walid Krati

Software Engineering Student • System Programming • C

GitHub:

https://github.com/wkratos77

⭐ If you found this project interesting, feel free to explore the implementation.

About

Custom implementation of printf in C, focused on variadic functions, format parsing, and modular output handling.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages