A lightweight reimplementation of the C standard library function printf — ft_printf prints formatted output to stdout.
This repository contains a minimal, portable implementation intended for learning and assessment (commonly used as a 42-school project).
Badges
Table of contents
- About
- Features
- Supported conversions & flags
- Requirements
- Installation / Build
- Usage
- Examples
- Testing
- Development notes
- Contributing
- License
- Author
- Contact
ft_printf is a small, educational implementation of printf. The goal is to reproduce the behavior of the standard printf for a subset of conversion specifiers and flags, handling formatting, field width and precision, and variable argument lists.
This project is useful for:
- Learning about variadic functions (stdarg.h)
- Practicing string formatting and buffer management in C
- Implementing and testing formatting logic for different data types
- Implements core printf-like formatting
- Supports integer, unsigned, hex, pointer, char and string conversions
- Handles field width and precision
- Handles left alignment and zero-padding
- Small, dependency-free C implementation suitable for embedding or testing
Included in this implementation:
- Conversion specifiers: %c, %s, %p, %d, %i, %u, %x, %X, %%
- Flags: None
Note: Behavior may differ in edge cases from the system printf. Consult the source for exact supported behavior and limitations.
- POSIX-compatible OS (Linux, macOS)
- C compiler (gcc, clang)
- make
Clone the repository and build with the provided Makefile:
git clone https://github.com/AdnilsonPinheiro/ft_printf.git
cd ft_printf
makeMake targets (may vary):
- make — build the library or binary
- make clean — remove object files
- make fclean — remove binaries and library
- make re — fclean + make
Call ft_printf as you would call printf:
#include "ft_printf.h" // path may vary
int main(void)
{
ft_printf("Hello %s, number: %d, hex: %#x\n", "world", 42, 255);
return 0;
}Some example outputs:
-
ft_printf("%%\n"); -> prints "%"
-
ft_printf("Char: %c\n", 'A');
-
ft_printf("String: %.5s\n", "HelloWorld"); -> prints "Hello"
-
ft_printf("Pointer: %p\n", ptr);
-
ft_printf("Hex: %08x\n", 255); -> prints "000000ff"
-
You can manually compare behavior to the system printf for many cases. Automating comparisons with a test harness is recommended to catch edge cases.
- Payed close attention to memory management: avoided leaks and buffer overruns.
- Worked incrementally: implemented conversions one-by-one and added unit tests.
- Used valgrind during development to detect invalid memory accesses:
Contributions are welcome. Suggested workflow:
- Fork the repository
- Create a feature branch: git checkout -b feat/description
- Add tests for new behavior
- Open a pull request describing the change
This repository is provided under the MIT License by default. Replace with your preferred license if different.
- Adnilson Pinheiro — https://github.com/AdnilsonPinheiro
For questions or improvements, open an issue or pull request in the repository.