Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ft_printf

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

  • Build: Build Status
  • License: License
  • Language: C

Table of contents

About

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

Features

  • 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

Supported conversions & flags

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.

Requirements

  • POSIX-compatible OS (Linux, macOS)
  • C compiler (gcc, clang)
  • make

Installation / Build

Clone the repository and build with the provided Makefile:

git clone https://github.com/AdnilsonPinheiro/ft_printf.git
cd ft_printf
make

Make targets (may vary):

  • make — build the library or binary
  • make clean — remove object files
  • make fclean — remove binaries and library
  • make re — fclean + make

Usage

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;
}

Examples

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.

Development notes

  • 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:

Contributing

Contributions are welcome. Suggested workflow:

  1. Fork the repository
  2. Create a feature branch: git checkout -b feat/description
  3. Add tests for new behavior
  4. Open a pull request describing the change

License

This repository is provided under the MIT License by default. Replace with your preferred license if different.

Author

Contact

For questions or improvements, open an issue or pull request in the repository.

About

A take on printf. Parsing was never such fun.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages