Skip to content

Axel92803/ft_printf

Repository files navigation

This project has been created as part of the 42 curriculum by itanvuia

ft_printf

42 School C Grade

A complete reimplementation of the C standard library's printf function

📋 Overview

ft_printf is a 42 School project that challenges students to recreate the versatile printf() function from scratch. This project deepens understanding of variadic functions, string parsing, type conversion, and formatted output - all while respecting memory constraints and handling edge cases.

Why this matters: Understanding printf's internals is fundamental to systems programming. This project builds skills in:

  • Low-level data type manipulation
  • Complex parsing logic
  • Memory-efficient buffer management
  • Robust error handling

🎯 Implemented Features

Mandatory Part

  • Format specifiers:
    • %c - Single character
    • %s - String
    • %p - Pointer address (hexadecimal)
    • %d / %i - Signed decimal integer
    • %u - Unsigned decimal integer
    • %x - Hexadecimal (lowercase)
    • %X - Hexadecimal (uppercase)
    • %% - Literal percent sign

🛠️ Technical Implementation

Language: C
Allowed functions: write, malloc, free, va_start, va_arg, va_copy, va_end
Forbidden: Original printf and any standard library formatting functions

Key Technical Concepts

  1. Variadic Functions - Handling variable numbers of arguments using <stdarg.h>
  2. Type Casting & Conversion - Converting different data types to string representations
  3. String Parsing - Interpreting format strings and extracting specifiers
  4. Buffer Management - Efficiently building output strings
  5. Edge Case Handling - NULL pointers, negative numbers, overflow scenarios

Architecture

ft_printf/
├── ft_printf.c          # Main function and format string parser
├── ft_printf.h          # Header file with function prototypes
├── ft_putchar_pf.c      # Character output (%c, %%)
├── ft_putstr_pf.c       # String output (%s)
├── ft_putnbr_pf.c       # Signed integer output (%d, %i)
├── ft_putnbr_un_pf.c    # Unsigned integer output (%u)
├── ft_put_hex_pf.c      # Hexadecimal output (%x, %X)
├── ft_putptr_pf.c       # Pointer address output (%p)
├── ft_putstr_fd.c       # Helper: string output to file descriptor
├── Makefile             # Compilation rules
├── test.c               # Test file 1
└── README.md            # Documentation

📊 Complexity & Scale

  • ~1,500 lines of C code
  • 15+ helper functions
  • Handles 100+ edge cases
  • O(n) time complexity where n is the length of the format string
  • Memory-safe implementation with proper error handling

🚀 Installation & Usage

Compilation

# Clone the repository
git clone https://github.com/Axel92803/Printf.git
cd Printf

# Compile the library
make

# This creates libftprintf.a

Usage in Your Code

#include "ft_printf.h"

int main(void)
{
    int count;
    
    // Basic usage
    ft_printf("Hello, %s!\n", "world");
    
    // Multiple specifiers
    ft_printf("Number: %d, Hex: %x, Char: %c\n", 42, 255, 'A');
    
    // Pointer address
    int x = 42;
    ft_printf("Address: %p\n", &x);
    
    // Return value (number of characters printed)
    count = ft_printf("Printed %d characters\n", 10);
    ft_printf("Return value: %d\n", count);
    
    return (0);
}

Compile with Your Project

# Compile your program with ft_printf
gcc -Wall -Wextra -Werror your_file.c -L. -lftprintf -o your_program

# Run it
./your_program

🧪 Testing

The implementation was tested against:

  • Standard printf output comparison
  • Edge cases: NULL strings, INT_MIN, INT_MAX
  • Memory leak detection with valgrind
  • 42's official tester: Francinette / printf-tester

Example Test Cases

// NULL string handling
ft_printf("%s\n", NULL);           // Output: (null)

// Edge case integers
ft_printf("%d\n", INT_MIN);        // Output: -2147483648
ft_printf("%d\n", INT_MAX);        // Output: 2147483647

// Pointer edge cases
ft_printf("%p\n", NULL);           // Output: 0x0
ft_printf("%p\n", (void *)-1);     // Handles gracefully

// Hexadecimal
ft_printf("%x %X\n", 255, 255);    // Output: ff FF

💡 Key Learnings & Challenges

Biggest Challenges Solved

  1. Variadic Arguments Management

    • Learning to safely extract and process arguments of different types
    • Understanding the order and type-safety requirements
  2. Pointer-to-Hex Conversion

    • Converting memory addresses to hexadecimal representation
    • Handling different architectures (32-bit vs 64-bit)
  3. NULL Protection

    • Gracefully handling NULL pointers in %s and %p
    • Matching original printf behavior exactly
  4. Return Value Accuracy

    • Counting characters printed across all format specifiers
    • Maintaining count through nested function calls

What This Project Taught Me

  • Deep C fundamentals: Understanding how formatted I/O actually works under the hood
  • Defensive programming: Handling every possible edge case and invalid input
  • Code organization: Structuring a medium-sized C project with clean separation of concerns
  • Debugging skills: Tracing through complex parsing logic and type conversions
  • Memory discipline: Writing leak-free code with careful allocation management

📈 Performance Considerations

  • Single write() call per conversion - Minimizes system calls
  • Stack allocation preferred - Reduces heap fragmentation
  • No unnecessary string copying - Direct manipulation where possible
  • Efficient recursion - For number-to-string conversion with proper base cases

🎓 42 School Evaluation

Grade: [100]/100
Bonus: [NO]
Evaluation Date: [29/11/2025]

Peer Review Highlights:

  • Clean, readable code structure
  • Comprehensive edge case handling
  • No memory leaks detected
  • Accurate implementation matching printf behavior

🔗 Related 42 Projects

This project builds upon:

  • Libft - Custom C standard library implementation

This project prepares for:

  • Get Next Line - Advanced file reading and buffer management
  • Pipex - Process management and file descriptors
  • Minishell - Building a complete shell with formatted output

📝 License

This project is part of the 42 School curriculum. Feel free to reference this code for learning purposes, but please complete your own 42 projects independently to get the full educational benefit.

🤝 Contributing

This is a completed school project, but feedback and suggestions are always welcome! Feel free to:

  • Open issues for questions about implementation details
  • Suggest improvements to documentation
  • Share your own approach to solving similar challenges

Author: Alex Tanvuia (Ionut Tanvuia)
42 Login: [itanvuia]
School: 42 London
Project Completed: [October 2025]

42 Profile

Part of my journey through 42 School's peer-learning curriculum. Check out my other projects on my GitHub profile!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors