Skip to content

SnowyCommander/ft_printf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This project has been created as part of the 42 curriculum by jiylee.

Description

  • For?

    Helping me understand how printf() works by implementing it myself, and learn about using a variable number of arguments, variadic functions in C.
  • Overview?

    This project involves recoding printf() for my programs in the future as assignments by 42school. Its version is 12.1 without the Bonus part.
  • What?

    printf(), variadic functions in C

Instructions

  • Installation

git clone <repository-url> working_directory
cd working_directory
  • Compilation

    • Type make in the commandline of where this Makefile exists, then you'll get "*.o" files and "libftprintf.a"
    • Type make clean to remove just "*.o" files.
    • Type make fclean which means fully-clean, to remove not only "*.o" files, but also "libftprintf.a".
    • Type make re to fully-clean then get "*.o" files and "libftprintf.a" again.
    • After you get "libftprintf.a", type cc -Wall -Wextra -Werror main.c libftprintf.a -o program to compile together.
    • Type ./program to execute.
  • Usage

#include "ft_printf.h"

int main(void)
{
	ft_printf("Hello %s, you are %d years old!\n", "World", 42);
	ft_printf("Hex: %x, Pointer: %p\n", 255, &main);
	return (0);
}

Compile with:

cc -Wall -Wextra -Werror main.c libftprintf.a -o program

Execute the program with:

./program

Supported Format Specifiers

Specifier Description
%c Single character
%s String
%p Pointer address in hexadecimal
%d Signed decimal integer
%i Signed integer
%u Unsigned decimal integer
%x Unsigned hexadecimal (lowercase)
%X Unsigned hexadecimal (uppercase)
%% Percent sign

Algorithm

Format String Parsing (Batch Write Approach)

  1. ft_printf scans the format string looking for %
  2. All characters before % are identified as a contiguous segment within the original format string and written in a single write() call
  3. When % is found, the next character is read and ft_handle_format is called
  4. ft_handle_format dispatches to the appropriate print function based on the specifier
  5. This continues until the end of the format string

Why Batch Write Approach?

  • Instead of calling write() one character at a time, consecutive plain-text characters up to the next % are written together in a single write() call
  • No separate memory buffer is allocated; the pointer to the original format string is used directly
  • Minimizes the number of write() system calls, reducing system call overhead

Number Conversion (Recursive Approach)

For %d, %u, %x, %X, %p, I use recursion to print digits in correct order:

  1. If n >= base, recursively call with n / base first
  2. Then print n % base as the current digit
  3. This ensures the most significant digit prints first

Why recursion?

  • Simple and elegant solution
  • Avoids needing a buffer to reverse digits
  • Stack naturally handles digit ordering

Data Structure

ft_printf/
├── ft_printf.c         ← ft_printf, ft_handle_format
├── ft_print_string.c   ← ft_print_char, ft_print_str
├── ft_print_hex.c      ← ft_print_hex, ft_print_hex_upper, ft_print_ptr
├── ft_print_nbr.c      ← ft_print_nbr
├── ft_printf.h         ← All function prototypes
└── Makefile

File Structure Justification

File Responsibility
ft_printf.c Main logic and format dispatching
ft_print_string.c Character and string output
ft_print_hex.c Hexadecimal and pointer output
ft_print_dec.c Decimal number output (signed/unsigned)

Why this separation?

  • Each file handles related functionality
  • Stays within Norm's 5 functions per file limit
  • Easy to maintain and extend

Resources

About

This project involves recoding printf() for my programs in the future as assignments by 42school. Its version is 12.1 without the Bonus part.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors