Skip to content
ibrahim0enesPublic

About

A recoded implementation of printf() in C — 42 School project. A static library (libftprintf.a) supporting the cspdiuxX% conversions, built using variadic functions (va_list).

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

8 Commits

Folders and files

Repository files navigation

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

ft_printf

Description

ft_printf is a simplified reimplementation of the C standard library printf function.

The project uses variadic functions to print formatted output and returns the number of characters written.

Supported conversions:

  • %c — character
  • %s — string
  • %p — pointer address in hexadecimal
  • %d / %i — signed decimal integer
  • %u — unsigned decimal integer
  • %x / %X — hexadecimal integer
  • %% — percent sign

Instructions

Build the static library:

make

This creates:

libftprintf.a

Use it in a program:

#include "ft_printf.h"

int	main(void)
{
	ft_printf("Hello %s: %d\n", "42", 42);
	return (0);
}

Compile your program with the library:

cc main.c -L. -lftprintf -o test
./test

Useful Makefile rules:

make        # Build libftprintf.a
make clean  # Remove object files
make fclean # Remove object files and library
make re     # Rebuild everything

Algorithm

ft_printf reads the format string from left to right.

  • Normal characters are printed directly with write.
  • When % is found, the next character selects the matching conversion.
  • Variadic arguments are read with va_list, va_start, va_arg, and va_end.
  • Each conversion function returns its number of written characters.
  • ft_printf adds these values and returns the final count.
  • If a write operation fails, the error is propagated as -1.

For numeric conversions, the number is repeatedly divided by its base (10 for decimal and 16 for hexadecimal). The remainder gives the last digit, so digits are stored temporarily and printed in reverse order.

Resources

AI Usage

AI was used as a learning assistant to explain variadic functions, format conversions, error handling, Norminette feedback, and to review code structure. The implementation was reviewed and understood by the author before use.

About

A recoded implementation of printf() in C — 42 School project. A static library (libftprintf.a) supporting the cspdiuxX% conversions, built using variadic functions (va_list).

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages