Skip to content

sdevsantiago/ft_printf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ft_printf

A reimplementation of the printf function in C.

Score Language
Last commit

ℹ️ About Project

The purpose of this project is to recode C's original function printf().

This library reimplements the core functionality of the standard printf() and fprintf() functions. It also implements the ft_eprintf() function.

Features:

  • Full variadic argument support: handles variable number of arguments using va_list
  • Comprehensive format specifiers: supports all basic printf conversion specifiers
  • Error handling: proper handling of malformed format strings

Supported Format Specifiers:

Specifier Type Description
%c Character Prints a single character
%s String Prints a null-terminated string
%d Integer Prints a signed decimal integer
%i Integer Prints a signed decimal integer (same as %d)
%u Unsigned Integer Prints an unsigned decimal integer
%o Octal Prints an unsigned integer in octal base
%x Hexadecimal Prints an unsigned integer in lowercase hexadecimal
%X Hexadecimal Prints an unsigned integer in uppercase hexadecimal
%p Pointer Prints a pointer address in hexadecimal format
%% Literal Prints a literal '%' character

🔧 Function Prototypes

int ft_printf(char const *format, ...);
int ft_fprintf(int fd, char const *format, ...);
int ft_eprintf(char const *format, ...);

Parameters:

  • format: Format string containing text and format specifiers
  • ...: Variable arguments corresponding to format specifiers
  • fd (for ft_fprintf): File descriptor to which output is written

Return value:

  • Number of characters printed on success
  • -1 on error (malformed format string)

For detailed info, refer to this project subject.

🚀 Getting Started

Prerequisites

  • GCC compiler
  • Make utility
  • Unix-like system (Linux, macOS, WSL)

Install prerequisites

  • Debian/Ubuntu

    sudo apt install build-essential

🔧 Build

  1. Clone the repository:

    git clone https://github.com/sdevsantiago/ft_printf.git
    cd ft_printf
  2. Compile the library:

    make         # Full compilation
    make clean   # Remove object files
    make fclean  # Remove all generated files
    make re      # Rebuild everything from scratch
  3. Clean build files:

    make clean  # Remove object files
    make fclean # Remove all generated files
    make re     # Rebuild everything from scratch

Available Make Targets

Command Description
make Compiles the ft_printf library (creates libftprintf.a)
make all Same as make
make clean Remove object files (*.o)
make fclean Remove object files and the library
make re Clean and rebuild everything

👨‍💻 Usage

Basic Usage

  1. Include the header in your C file:

    #include "ft_printf.h"
  2. Compile your program with the library:

    cc -Wall -Wextra -Werror your_file.c path/to/ft_printf/libftprintf.a -I path/to/ft_printf/ -o your_program

Example usage

#include "ft_printf.h"
#include <fcntl.h>

int main(void)
{
    // Basic examples
    ft_printf("Hello, World!\n");
    ft_printf("Character: %c\n", 'A');
    ft_printf("String: %s\n", "42 School");
    ft_printf("Number: %d\n", 42);
    ft_printf("Unsigned: %u\n", 3000000000U);
    ft_printf("Hexadecimal: %x %X\n", 255, 255);
    ft_printf("Octal: %o\n", 64);
    ft_printf("Pointer: %p\n", &main);
    ft_printf("Percentage: %%\n");

    // Mixed format specifiers
    ft_printf("User %s has %d points and %u coins\n", "Alice", -50, 1500);

    // Hexadecimal formatting
    ft_printf("Memory address: %p, Value in hex: 0x%x\n", ptr, value);

    // Error handling
    int chars_printed = ft_printf("%", 42);
    if (chars_printed == -1)
    {
        // Handle error
    }

    int fd = open("file", O_WRONLY);
    if (fd != -1)
    {
        ft_fprintf(fd, "Hello %d\n", 42);
    }
    else
    {
        ft_eprintf("error: unable to write on file\n");
    }
    return (0);
}

📏 Norminette

The code strictly complies with 42's Norminette v4:

norminette *.c *.h

More info in the official Norminette repository.

🙇‍♂️ Special thanks

⚖️ License

This work is published under the terms of 42 Unlicense. This means you are free to use, modify, and share this software.

About

A reimplementation of the printf function in C.

Topics

Resources

License

Stars

Watchers

Forks