Skip to content

Repository files navigation

ft_printf

42 ft_printf C Completed

About

ft_printf is a custom implementation of the standard C printf function.

The project introduces variadic functions and formatted output processing by recreating the core behavior of printf without relying on the original implementation.

It was completed as part of the 42 / 1337 Common Core.

Features

The function supports the following conversions:

Conversion Description
%c Prints a single character
%s Prints a string
%p Prints a pointer address in hexadecimal
%d Prints a signed decimal integer
%i Prints a signed integer
%u Prints an unsigned decimal integer
%x Prints a hexadecimal integer using lowercase letters
%X Prints a hexadecimal integer using uppercase letters
%% Prints a percent sign
%o Prints an unsigned integer in octal

Function Prototype

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

The function returns the total number of characters written, matching the behavior of the standard printf function.

Compilation

Clone the repository:

git clone https://github.com/wkratos/ft_printf.git
cd ft_printf

Build the static library:

make

This creates:

libftprintf.a

Other available rules:

make clean
make fclean
make re

Usage

Include the header in your program:

#include "ft_printf.h"

Example:

#include "ft_printf.h"

int	main(void)
{
	int	count;

	count = ft_printf(
			"Character: %c\nString: %s\nNumber: %d\nHex: %#x\n",
			'A',
			"Hello, 42!",
			1337,
			1337
			);
	ft_printf("Printed characters: %d\n", count);
	return (0);
}

Compile your program with the library:

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

Run it:

./ft_printf_test

Implementation Overview

The implementation processes the format string one character at a time.

When a % character is found, the conversion identifier is parsed and dispatched to the appropriate output function.

The project includes logic for:

  • variadic argument handling with va_list;
  • signed and unsigned number conversion;
  • hexadecimal and octal conversion;
  • pointer formatting;
  • prefix and sign handling;
  • accurate return-value calculation;
  • safe handling of null strings.

Project Structure

.
├── Makefile
├── ft_printf.c
├── ft_printf.h
├── source files
└── README.md

The exact file organization may differ depending on the implementation.

Testing

You can compare the output and return value of ft_printf with the original printf:

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

int	main(void)
{
	int	original;
	int	custom;

	original = printf("Original: %#x %+d %s\n", 42, 42, "test");
	custom = ft_printf("Custom:   %#x %+d %s\n", 42, 42, "test");

	printf("printf returned: %d\n", original);
	printf("ft_printf returned: %d\n", custom);
	return (0);
}

Recommended cases to test include:

  • zero;
  • positive and negative integers;
  • INT_MIN and INT_MAX;
  • UINT_MAX;
  • null strings;
  • null pointers;
  • consecutive conversions;
  • literal percent signs;
  • hexadecimal and octal prefixes.

Learning Outcomes

This project provided practical experience with:

  • variadic functions;
  • formatted output parsing;
  • number-base conversion;
  • static libraries;
  • modular C programming;
  • edge-case handling;
  • reproducing standard-library behavior.

Author

Walid Krati


This repository contains my implementation of the 42 ft_printf project. It is intended as a portfolio and educational reference. Students are encouraged to understand and implement the project independently.

About

A custom implementation of C's printf function, built from scratch as part of the 42 Common Core.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages