Skip to content

anarita99/ft_printf

Repository files navigation

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

ft_printf

Description

ft_printf is a project that recreates the behavior of the standard C library function printf. The goal is to implement a variadic function that formats and prints data to standard output, handling multiple format specifiers. This project introduces fundamental concepts such as variadic functions, type conversion, and formatted output in C.

Instructions

Compilation

To compile the project, run:

make

This creates a static library libftprintf.a that can be linked to your programs.

Usage

Link the library when compiling:

gcc your_program.c libftprintf.a

Function Prototype

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

Returns the number of characters printed (excluding the null terminator), or -1 on error.

Supported Format Specifiers

Specifier Description Example
%c Print a single character ft_printf("%c", 'A') → A
%s Print a string ft_printf("%s", "hello") → hello
%p Print a pointer address in hexadecimal ft_printf("%p", ptr) → 0x7ffe5367e044
%d Print a signed decimal integer ft_printf("%d", -42) → -42
%i Print a signed decimal integer ft_printf("%i", 123) → 123
%u Print an unsigned decimal integer ft_printf("%u", 42) → 42
%x Print a number in lowercase hexadecimal ft_printf("%x", 255) → ff
%X Print a number in uppercase hexadecimal ft_printf("%X", 255) → FF
%% Print a percent sign ft_printf("%%") → %

Examples

#include "ft_printf.h"

int main(void)
{
    int count;
    
    // Character and string
    ft_printf("Character: %c\n", 'A');
    ft_printf("String: %s\n", "Hello, World!");
    
    // Numbers
    ft_printf("Decimal: %d\n", -42);
    ft_printf("Unsigned: %u\n", 42);
    
    // Hexadecimal
    ft_printf("Hex (lower): %x\n", 255);
    ft_printf("Hex (upper): %X\n", 255);
    
    // Pointer
    int x = 42;
    ft_printf("Pointer: %p\n", &x);
    
    // Percent sign
    ft_printf("Percentage: 100%%\n");
    
    // Return value
    count = ft_printf("Characters printed: ");
    ft_printf("%d\n", count);
    
    return (0);
}

Output:

Character: A
String: Hello, World!
Decimal: -42
Unsigned: 42
Hex (lower): ff
Hex (upper): FF
Pointer: 0x7ffe5367e044
Percentage: 100%
Characters printed: 21

Implementation Details

Variadic Functions

The project uses variadic functions to handle a variable number of arguments:

  • va_list: Type to hold information about variable arguments
  • va_start: Initialize a va_list to retrieve arguments
  • va_arg: Retrieve the next argument
  • va_end: Clean up the va_list

Format Parsing

The function parses the format string character by character:

  1. Regular characters are printed directly
  2. When % is encountered, the next character determines the format specifier
  3. Appropriate helper function is called based on the specifier
  4. Return value accumulates the total number of characters printed

Edge Cases Handled

  • NULL strings: Prints (null) for %s with NULL pointer
  • NULL pointers: Prints (nil) for %p with NULL pointer
  • Invalid format: Returns -1 if format string ends with %
  • Write errors: Returns -1 if write system call fails

Project Structure

ft_printf/
├── ft_printf.c         # Main printf logic and format parsing
├── ft_printchar.c      # Print single character
├── ft_printstr.c       # Print string
├── ft_printnbr.c       # Print signed integer
├── ft_unsputnbr.c      # Print unsigned integer
├── ft_hexaputnbr.c     # Print hexadecimal numbers
├── ft_printp.c         # Print pointer addresses
├── ft_printf.h         # Header file with prototypes
└── Makefile            # Build automation

Function Breakdown

Main Function

  • ft_printf: Main function that parses format string and dispatches to appropriate handlers

Helper Functions

  • ft_printchar: Handles %c - prints a single character
  • ft_printstr: Handles %s - prints a string (handles NULL)
  • ft_printnbr: Handles %d and %i - prints signed integers (handles negative numbers)
  • ft_unsputnbr: Handles %u - prints unsigned integers
  • ft_hexaputnbr: Handles %x and %X - prints hexadecimal (lowercase and uppercase)
  • ft_printp / ft_checkp: Handles %p - prints pointer addresses with "0x" prefix

Testing

Test your implementation with various format specifiers:

#include "ft_printf.h"

int main(void)
{
    ft_printf("Character: %c\n", 'A');
    ft_printf("String: %s\n", "Hello");
    ft_printf("Number: %d\n", 42);
    ft_printf("Hex: %x\n", 255);
    ft_printf("Pointer: %p\n", &main);
    
    return (0);
}

Compare output and return values with the standard printf to verify correctness.

Resources

The main resource that contributed to my understanding of this project was discussing variadic functions and implementation strategies with older students at 42.

Additional references that were helpful include:

AI tools were used to assist with the writing and structuring of this README file.


About

A custom implementation of the printf function that handles multiple format specifiers and prints formatted output to standard output.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors