Skip to content
youssef-benchaabounPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

This project has been created as part of the 42 curriculum by yoben-ch.

ft_printf

Description

ft_printf is a small C library that recreates the mandatory behavior of the standard printf function for the 42 curriculum. The project focuses on variadic functions, formatted output, integer base conversion, pointer formatting, and accurate return values.

The library builds as libftprintf.a and exposes this function:

int ft_printf(const char *format, ...);

Supported mandatory conversions:

  • %c prints one character.
  • %s prints a string, using (null) for a null pointer.
  • %p prints a pointer in hexadecimal format, using (nil) for a null pointer on this platform.
  • %d and %i print signed decimal integers.
  • %u prints unsigned decimal integers.
  • %x prints lowercase hexadecimal integers.
  • %X prints uppercase hexadecimal integers.
  • %% prints a percent sign.

Bonus flags, width, and precision are not implemented.

Instructions

Compile the library:

make

This creates libftprintf.a at the repository root.

Clean object files:

make clean

Remove object files and the library:

make fclean

Rebuild from scratch:

make re

Example usage:

#include "ft_printf.h"

int main(void)
{
    ft_printf("Hello %s, value = %d\n", "42", 42);
    return (0);
}

Compile with the library:

cc -Wall -Wextra -Werror main.c libftprintf.a -I.

Algorithm and Data Structures

The project uses a linear parser over the format string. Each normal character is written directly to standard output with write. When the parser finds a % followed by a supported conversion specifier, it dispatches to a small helper function for that conversion.

The main data structure is va_list, from <stdarg.h>, which stores the current position in the variable argument list. ft_printf initializes it with va_start, extracts arguments with va_arg, and releases it with va_end.

Numbers are printed recursively. For decimal output, the number is divided by 10 until the most significant digit is reached, then digits are written while the recursion unwinds. Unsigned decimal uses the same idea without sign handling. Hexadecimal output uses the same recursive division strategy with base 16 and a base string (0123456789abcdef or 0123456789ABCDEF) to choose each digit.

This approach avoids dynamic allocation, keeps memory management simple, and matches the subject requirement not to reproduce libc printf buffer management. Each helper returns the number of characters it wrote, and ft_printf accumulates those values to produce the final return count.

Resources

Useful references for this topic:

  • man 3 printf
  • man 3 stdarg
  • man 2 write
  • The 42 ft_printf subject PDF included in this repository
  • cppreference: Variadic arguments in C
  • The GNU C Library manual: Formatted Output

AI use: AI assistance was used to read and summarize the subject requirements, run build and behavior checks, and draft this README. The implementation files were reviewed against the subject, but no bonus behavior was added.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages