Skip to content

migclay12/ft_printf

Repository files navigation

ft_printf - Custom Printf Implementation

Build Status C License 42 School

📝 Description

ft_printf is a custom implementation of the standard C library function printf. This project recreates the behavior of printf with support for the most common format specifiers and flags.

This project was developed as part of the 42 School curriculum to demonstrate understanding of variadic functions, string formatting, and low-level programming concepts in C.

🚀 Features

  • Character output (%c)
  • String output (%s)
  • Integer output (%d, %i)
  • Unsigned integer output (%u)
  • Hexadecimal output (%x, %X)
  • Pointer output (%p)
  • Percent literal (%%)
  • Null string handling
  • Return value matching standard printf

📋 Supported Format Specifiers

Specifier Description Example
%c Character ft_printf("%c", 'A')
%s String ft_printf("%s", "Hello")
%d Signed decimal integer ft_printf("%d", -42)
%i Signed integer (same as %d) ft_printf("%i", 42)
%u Unsigned decimal integer ft_printf("%u", 12345)
%x Hexadecimal (lowercase) ft_printf("%x", 255)
%X Hexadecimal (uppercase) ft_printf("%X", 255)
%p Pointer address ft_printf("%p", &variable)
%% Percent literal ft_printf("%%")

🔧 Installation & Usage

Prerequisites

  • GCC compiler (or any C compiler)
  • Make build system

Building the Library

  1. Clone the repository:

    git clone <repository-url>
    cd ft_printf
  2. Build the library:

    make

    This creates libftprintf.a

  3. Build and run tests:

    make test

Usage in Your Code

  1. Include the header:

    #include "ft_printf.h"
  2. Link with the library:

    gcc your_program.c -L. -lftprintf -o your_program
  3. Use in your code:

    #include "ft_printf.h"
    
    int main(void) {
        ft_printf("Hello, %s! The answer is %d\\n", "world", 42);
        return (0);
    }

🧪 Testing

The project includes comprehensive tests to verify functionality against the standard printf. Run tests with:

make test

This compares output and return values between the custom implementation and the standard library function.

📁 Project Structure

ft_printf/
├── ft_printf.c      # Main printf function implementation
├── ft_printf.h      # Header file with function declarations
├── utils.c          # Utility functions (strlen, putchar, putstr)
├── ft_putnbr.c      # Number and base conversion functions
├── test_ft_printf.c # Test program
├── Makefile         # Build configuration
└── README.md        # This file

🏗️ Implementation Details

Core Functions

  • ft_printf(): Main function that processes format strings and arguments
  • ft_putnbr(): Handles signed integer output
  • ft_putnbr_base(): Handles base conversions (decimal, hex, etc.)
  • ft_pointr(): Handles pointer address output
  • Utility functions for character and string output

Key Features

  • Variadic function handling using va_list, va_start, va_arg
  • Recursive number conversion for efficient digit processing
  • Base conversion support for multiple number formats
  • Proper return value counting matching standard printf behavior

🔍 Technical Details

Memory Management

  • No dynamic memory allocation
  • Uses static buffers for number conversion
  • Efficient character-by-character output

Error Handling

  • Handles null strings gracefully
  • Proper handling of negative numbers
  • Robust base conversion with validation

Performance

  • Optimized for 42 School coding standards
  • Minimal function call overhead
  • Efficient string and number processing

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

📜 License

This project is licensed under the MIT License - see the LICENSE file for details.

👨‍💻 Author

Miguel González Clayton


This project demonstrates fundamental C programming concepts including variadic functions, string manipulation, and low-level I/O operations.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors