Skip to content
ensui-devPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

This project has been created as part of the 42 curriculum by mju-ferr.

ft_printf

Description

ft_printf is a recreation of the standard C library function printf(). This project is designed to deepen understanding of variadic functions in C and help consolidate knowledge from other projects, through the implementation of a versatile and well-structured output formatting function.

The goal is to implement a function that mimics the behavior of the original printf(), handling various format specifiers and returning the number of characters printed.

Instructions

Compilation

To compile the project, simply run:

make

This will generate the libftprintf.a library file.

Cleaning

  • To remove object files: make clean
  • To remove object files and the library: make fclean
  • To recompile everything: make re

Usage

Include the header file in your C program:

#include "ft_printf.h"

Link the library when compiling your program:

cc your_program.c -L. -lftprintf

Example

#include "ft_printf.h"

int main(void)
{
    ft_printf("Hello %s!\n", "World");
    ft_printf("Number: %d\n", 42);
    ft_printf("Hex: %x\n", 255);
    return (0);
}

Supported Conversions

  • %c - Print a single character
  • %s - Print a string
  • %p - Print a pointer in hexadecimal format
  • %d - Print a decimal (base 10) number
  • %i - Print an integer in base 10
  • %u - Print an unsigned decimal (base 10) number
  • %x - Print a number in hexadecimal (base 16) lowercase
  • %X - Print a number in hexadecimal (base 16) uppercase
  • %% - Print a percent sign

Algorithm and Data Structure

Approach

The implementation uses a parser-based approach with the following structure:

  1. Main Parser Loop: The ft_printf() function iterates through the format string character by character
  2. Format Handler: When a % is encountered, the ft_handle_format() function is called to process the conversion specifier
  3. Specialized Converters: Each conversion type has its own dedicated function for clarity and maintainability

Key Design Decisions

Variadic Arguments: Uses the <stdarg.h> library functions:

  • va_start() - Initialize the argument list
  • va_arg() - Retrieve the next argument
  • va_end() - Clean up the argument list

Character Count Tracking: Each function returns the number of characters printed, which are accumulated and returned by ft_printf()

Edge Case Handling:

  • NULL string pointers print "(null)"
  • NULL pointers print "(nil)"
  • Negative numbers are handled with sign conversion

File Structure

  • ft_printf.c - Main function and format handler
  • ft_printf_char.c - Character and string conversions
  • ft_printf_nbr.c - Integer and unsigned integer conversions
  • ft_printf_hex.c - Hexadecimal and pointer conversions
  • ft_printf.h - Header file with function prototypes

Resources

Documentation

Tutorials

AI Usage

AI was used as a learning assistant for:

  • Understanding the technical details of variadic functions in C
  • Clarifying edge cases for different conversion specifiers (e.g., NULL pointer behavior)

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages