Skip to content

JosepharDev/ft_printf

Repository files navigation

ft_printf

A lightweight reimplementation of printf from the 42 curriculum, built as a static library: libftprintf.a.

This version focuses on mandatory conversion specifiers and writes directly to standard output using write(2).

Features

  • Supports: %c, %s, %p, %d, %i, %u, %x, %X, %%
  • Returns the total number of printed characters
  • Handles NULL strings as (null)
  • Prints pointers in lowercase hexadecimal with 0x prefix
  • Built with strict flags: -Wall -Wextra -Werror

Project Structure

ft_printf/
├── Makefile
├── ft_printf.h
├── ft_printf.c
├── ft_putchar.c
├── ft_putstr.c
├── ft_putnbr.c
├── ft_putnbr_un.c
├── ft_puthx.c
└── ft_putadrs.c

Build

From ft_printf/:

make

This generates:

  • libftprintf.a

Other targets:

make clean
make fclean
make re

Public API

Declared in ft_printf.h:

int	ft_printf(const char *str, ...);

Helper functions are also exposed in the header and used internally:

  • ft_putchar
  • ft_putstr
  • ft_putnbr
  • ft_putnbr_un
  • ft_puthx
  • ft_putadrs

Supported Conversions

Specifier Meaning
%c character
%s string
%p pointer address (0x...)
%d / %i signed decimal integer
%u unsigned decimal integer
%x unsigned hexadecimal (lowercase)
%X unsigned hexadecimal (uppercase)
%% literal %

Usage

1) Include the header

#include "ft_printf.h"

2) Build your program with the library

If you are compiling from inside ft_printf/:

cc -Wall -Wextra -Werror main.c libftprintf.a -o app

If ft_printf/ is a subfolder of your project:

cc -Wall -Wextra -Werror main.c -L./ft_printf -lftprintf -I./ft_printf -o app

Example

#include "ft_printf.h"

int main(void)
{
	int count;

	count = ft_printf("Char: %c | Str: %s | Num: %d | Hex: %x | Ptr: %p | %%\n",
		'A', "hello", -42, 255, "hello");
	ft_printf("Printed chars: %d\n", count);
	return (0);
}

Implementation Notes (Current Repository)

  • Output is written to file descriptor 1.
  • Write errors propagate as -1 from ft_printf.
  • Unknown specifiers after % are printed as-is (for example, %q prints q).
  • A trailing % at end of format string does not print anything and does not error.
  • ft_putnbr_un internally calls ft_putnbr for recursion steps; output remains correct for unsigned values.

Limitations

This implementation does not include standard printf flags or advanced formatting features, such as:

  • width
  • precision
  • left/right alignment
  • zero padding
  • +, space, # flags
  • length modifiers (hh, h, l, ll, etc.)

License

No license file is currently present in this folder. If you plan to share or reuse this project publicly, add a license file (for example, MIT).

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors