Skip to content

Repository files navigation

ft_printf

A custom printf implementation built as a static C library for the 42 curriculum.

42 badge Language badge Status badge

variadic arguments • formatted output • static library • write(2)


📌 Overview

ft_printf is a project developed as part of the 42 curriculum.

In the current repository state, the project builds libftprintf.a and implements a subset of the standard printf behavior. The dispatcher in ft_printf.c currently supports %c, %s, %p, %d, %i, %u, %x, %X, and %%, writes to standard output, and returns the number of printed characters.

This project focuses on:

  • variadic argument handling with va_list
  • parsing format specifiers inside a format string
  • converting signed, unsigned, hexadecimal, and pointer values
  • packaging the implementation as a reusable static library

✨ Features

  • ✅ Supports %c, %s, %p, %d, %i, %u, %x, %X, and %%
  • ✅ Returns the number of printed characters, like printf
  • ✅ Handles NULL strings by printing (null)
  • ✅ Prints pointer addresses with the 0x prefix
  • ✅ Supports both lowercase and uppercase hexadecimal output
  • ⚠️ The current implementation does not handle flags, width, or precision

🧠 Concepts Covered

Through this project, the following concepts are practiced:

  • variadic functions with va_start, va_arg, and va_end
  • low-level output with write
  • recursive number printing
  • base 10 and base 16 conversion
  • static library creation with ar
  • API exposure through a public header

🛠️ Build

From the project root, compile the library with:

make

Available Makefile rules:

make
make clean
make fclean
make re

Additional notes:

  • The default target creates libftprintf.a.
  • main.c is a local test file and is not included in the library build.
  • The files compiled into the library are the ones listed in FT_PRINTF inside Makefile.

🚀 Usage

Since the repository builds a library, the simplest way to try it is to link it with the provided main.c test file:

make
cc -Wall -Wextra -Werror main.c libftprintf.a -I ./ -o test_printf
./test_printf

Examples

cc -Wall -Wextra -Werror main.c libftprintf.a -I ./ -o test_printf
./test_printf
cc -Wall -Wextra -Werror your_file.c libftprintf.a -I ./ -o your_program

Additional notes:

  • Public function: int ft_printf(const char *type, ...);
  • Supported conversions in the current code are %c, %s, %p, %d, %i, %u, %x, %X, and %%.

📂 Project Structure

.
├── Makefile
├── ft_printf.h
├── ft_printf.c
├── ft_putchar.c
├── ft_putstr.c
├── ft_putnbr2.c
├── ft_printunsigned_decimal.c
├── ft_printhexa_low.c
├── ft_printhexa_upper.c
├── ft_printadresse.c
├── main.c
├── ft_putnbr.c
├── ft_printmodulo.c
├── printf.h
├── template.md
└── README.md

Structure Details

  • Repository root: flat layout containing sources, headers, the Makefile, and local test files
  • ft_printf.c: main parser and dispatcher for supported format specifiers
  • ft_putchar.c, ft_putstr.c, ft_putnbr2.c, ft_printunsigned_decimal.c, ft_printhexa_low.c, ft_printhexa_upper.c, ft_printadresse.c: helper functions used by the library build
  • main.c: manual comparison file against the standard printf
  • ft_putnbr.c, ft_printmodulo.c, and printf.h: present in the repository but not referenced by the current SRCS list in Makefile

⚙️ Project Constraints

for this project:

  • handles important edge cases such as NULL strings, NULL pointers, and INT_MIN
  • reuses generic helpers to avoid duplicated logic, especially for hexadecimal output
  • built with a simple and readable parsing flow
  • designed to stay compliant with 42 project constraints and Norm rules
  • limits its implemented scope to the conversion set listed above

🧪 Testing

The current repository supports the following testing approach:

  • library build with make
  • manual comparison against the standard printf via main.c
  • output checks for %c, %s, %p, %x, %X, %d, %u, and %%
  • return value checks printed by the local test program
  • basic NULL, zero, and unsigned edge-case coverage in the provided examples

Manual test examples

make
cc -Wall -Wextra -Werror main.c libftprintf.a -I ./ -o test_printf
./test_printf

Memory checks

valgrind --leak-check=full --track-origins=yes ./test_printf

📖 What I Learned

A project like this is useful for improving in the following areas:

  • thinking in terms of parsing and dispatch
  • separating formatting responsibilities into focused helper functions
  • reasoning about returned character counts, not only printed output
  • building and linking a reusable static library

🚧 Possible Improvements

Although the current repository builds and runs, several improvements could be considered:

  • add support for flags, width, precision, and additional specifiers
  • add automated regression tests instead of relying only on manual comparison
  • reorganize the repository into src/ and includes/ directories
  • remove or consolidate files that are not part of the current build

👤 Author

kpourcel
42 student


📚 Sources

  • Makefile for the build rules and compiled source list
  • main.c for the manual testing workflow and usage examples
  • ft_putstr.c and ft_printadresse.c for NULL output behavior details

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages