Skip to content

alexspopa/42-ft_printf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

ft_printf

Description

This project is a reimplementation of the standard C library function printf. It handles the basic cspdiuxX% conversions, mimicking the behaviour of the original.

Supported Conversions

Specifier Description
%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.

Instructions

  1. Clone the repository.
  2. Run make to compile the library libftprintf.a.
  3. Include the header ft_printf.h in your project and link the library.

Example usage:

#include "ft_printf.h"

int main() {
    int len = ft_printf("Hello %s, number %d\n", "World", 42);
    ft_printf("Printed %d characters\n", len);
    return (0);
}

Implementation

  • Algorithm: The function iterates linearly through the format string. Standard characters are written immediately to the output. When a % is encountered, a dispatcher function identifies the flag and calls the specific printing function using va_arg.
  • Data Structure: va_list (from <stdarg.h>) is used to manage the variable number of arguments.
  • Recursion: To print numbers (%d, %x, etc.) without buffer management (as forbidden by the subject), a recursive approach is used. This allows printing digits in the correct order directly to the standard output without memory allocation.

Resources

  • To study how the variadic functions behaved i used this YT video from the channel CodeVault.
  • man va_(start, arg, end)
  • This tester made by Tripouille.

About

A useful function to add to our libft.h

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors