Skip to content

LordMikkel/Ft_printf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

16 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ft_printf

alt text

πŸ–‹οΈ A Custom printf Implementation

Welcome to ft_printf, a project developed as part of the 42 curriculum. This is a handcrafted implementation of the standard printf function in C, designed to deepen understanding of variadic functions, format specifiers, and low-level output formatting.


πŸš€ Features

βœ… Handles standard format specifiers:

  • %c β†’ Character
  • %s β†’ String
  • %d / %i β†’ Integer
  • %u β†’ Unsigned Integer
  • %p β†’ Pointer Address
  • %x / %X β†’ Hexadecimal (lower/uppercase)
  • %% β†’ Print a literal %

βœ… Bonus Implementation: Supports essential formatting flags:

  • - β†’ Left-align output
  • 0 β†’ Zero-padding
  • # β†’ Prefix for hexadecimal (0x, 0X)
  • + β†’ Force display of sign for numeric values
  • (space) β†’ Add a space before positive numbers
  • .precision β†’ Control string truncation & numeric precision

⚠️ Limitations:

  • ❌ Does not support floating-point numbers (%f, %e, %g).
  • ❌ Does not support octal (%o).

πŸ“‚ Project Structure

ft_printf
β”œβ”€β”€ includes/         # Header files
β”œβ”€β”€ [libft](https://github.com/LordMikkel/libft)/            # Custom string & memory functions
β”œβ”€β”€ src/              # Core implementation of ft_printf
β”‚   β”œβ”€β”€ ft_printf.c   # Main function
β”‚   β”œβ”€β”€ utils/        # Helper functions for formatting
β”œβ”€β”€ src_bonus/        # Extended flags functionality
β”‚   β”œβ”€β”€ ft_printf_bonus.c
β”‚   β”œβ”€β”€ utils/        # Bonus-specific utilities
β”œβ”€β”€ Makefile          # Build automation
└── README.md         # You're reading it!

πŸ”§ Installation & Usage

1️⃣ Clone the repository

git clone git@github.com:LordMikkel/ft_printf.git
cd ft_printf

2️⃣ Compile the library

make

This will generate libftprintf.a, a static library that can be linked to your project.

3️⃣ Usage Example

#include "ft_printf.h"

int main()
{
    ft_printf("Hello, %s! You have %d new messages.\n", "Mikel", 42);
    return 0;
}

πŸ”¨ Makefile Rules

The Makefile includes a set of rules that help streamline the project build process and testing:

  • πŸ“¦ make: Compile the core project

Compiles the project and generates the libftprintf.a static library. This is the foundation for your ft_printf implementation.

  • πŸ† make bonus: Enable bonus features

Compiles and links the bonus features of the project, enabling additional formatting options such as custom flags (e.g., Min_width, -, 0, #, +, etc.). This produces a version of the library with these extended functionalities.

  • πŸ§ͺ make test: Run basic tests

Compiles and runs a comprehensive suite of tests for basic and complex formats. The test suite ensures your ft_printf works as expected, comparing it against the standard printf implementation. Tests cover characters, strings, integers, hexadecimals, and combinations.

  • πŸ§ͺ make testbonus: Run bonus tests

Compiles and runs tests specifically designed for the bonus features. These tests verify that flags such as Min_width, -, 0, #, +, and space are handled correctly. Bonus functionality ensures your ft_printf is more flexible and robust.

  • 🧹 make clean: Clean up object files

Removes all object files (.o) from the src/ and src_bonus/ directories, cleaning up intermediate build files to prepare for fresh compilations.

  • 🧹 make fclean: Full cleanup

Removes both object files and the compiled static library (libftprintf.a). This ensures the project is entirely cleaned, removing all generated files.

  • πŸ”„ make re/rebonus: Rebuild everything

Performs a complete rebuild of the project from scratch by running make fclean followed by make. This is perfect when you need to ensure that all files are freshly compiled.

alt text


🎯 Example Test Results

In my main.c, several tests are executed to check the correctness of the ft_printf implementation. The tests encompass a variety of formats and edge cases, ensuring comprehensive coverage. Here's an overview:

πŸ”  Basic Character and String Tests

  • Characters: Test for basic characters like 'A', null characters (\0), and special characters like newline (\n).
  • Strings: Tests for normal strings, empty strings, and long strings to check memory handling and performance.

πŸ§‘β€πŸ’» Pointer Tests

  • Tests for printing valid pointer addresses and handling NULL pointers.

πŸ’― Integer and Number Tests

  • Integers: Tests for positive and negative integers, as well as boundary values like INT_MAX and INT_MIN.
  • Unsigned Integers: Tests for unsigned integers, including edge cases like UINT_MAX.

πŸ”’ Hexadecimal Tests

  • Verifying the correct representation of hexadecimal numbers in both lowercase (%x) and uppercase (%X).

πŸ”€ Combination of Formats

  • Tests involving multiple format specifiers in a single print statement, ensuring that combinations work as expected.

🎁 Bonus Tests (If Defined)

  • Tests for additional flags such as Min_width, -, 0, #, +, and space. These flags offer more control over the printed output.
  • Tests with combinations of flags ensure that they work correctly when used together, e.g., %-10.5d, %010d, and %#x.

πŸ§ͺ Test Output

Each test compares the output of ft_printf with the standard printf output. If the outputs match, the test passes; otherwise, it fails. At the end of the test suite, a summary will display:

  • Passed Tests: Displayed in green 🟩.
  • Failed Tests: Displayed in red πŸŸ₯.

☠️ Final Boss

This printf implementation passes all the tests from ultimate ft_printf_test when you execute the 938 tests, your terminal may display an overwhelming number of warningsβ€”don’t panic! Just let it run, and in the end, you’ll see the final test results.

alt text


πŸ† Credits

Developed by Mikel Garrido as part of the 42 curriculum.

I want to thank madebypixel02 for the super cool progress bars, cacharle for the ultimate test, and vdaniely for the initial main

πŸ”— GitHub: LordMikkel

About

Ft_Printf with Bonus πŸ–¨οΈ: A custom implementation of printf in C from scratch.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published