Skip to content

Latest commit

 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

ft_printf

Description

A recreation of the C standard library printf() function, implemented as a static library libftprintf.a. The main goal of this project is to understand how variadic arguments (va_args) work in C.

The library handles the following format specifiers: %c, %s, %p, %d, %i, %u, %x, %X, %%.

Instructions

make        # compile the library
make clean  # remove object files
make fclean # remove object files and library
make re     # recompile from scratch

To use in your own project:

#include "ft_printf.h"
# link with: cc your_file.c libftprintf.a

Algorithm and Data Structure

The format string is parsed character by character in a single loop. When a % is encountered, the next character determines which conversion to apply via a dispatch function (switch_type). Each conversion reads the next variadic argument using va_arg and writes the output directly with write(), counting bytes as it goes. The function returns the total number of characters printed, mirroring the behavior of the original printf().

All logic lives in a single file. This approach trades reusability for compactness. Here are no shared helpers to extract across files, which made it easier to reason about the whole implementation at once while learning. The tradeoff is that functions like character output and string output are tightly coupled, but for a project of this scope that is acceptable.

Numbers are printed recursively: divide by the base, recurse on the quotient, then print the remainder digit. This avoids needing a buffer to reverse digits manually. Hexadecimal and unsigned decimal share the same recursive function (put_hex), parameterized by base and case.

Resources

AI usage: Claude was used as a learning tool to understand variadic argument mechanics and to help write this README file, in accordance with the 42 global AI usage policy.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages