This project has been created as part of the 42 curriculum by adores.
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.
To compile the project, run:
makeThis creates a static library libftprintf.a that can be linked to your programs.
Link the library when compiling:
gcc your_program.c libftprintf.aint ft_printf(const char *format, ...);Returns the number of characters printed (excluding the null terminator), or -1 on error.
| 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("%%") → % |
#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
The project uses variadic functions to handle a variable number of arguments:
va_list: Type to hold information about variable argumentsva_start: Initialize ava_listto retrieve argumentsva_arg: Retrieve the next argumentva_end: Clean up theva_list
The function parses the format string character by character:
- Regular characters are printed directly
- When
%is encountered, the next character determines the format specifier - Appropriate helper function is called based on the specifier
- Return value accumulates the total number of characters printed
- NULL strings: Prints
(null)for%swith NULL pointer - NULL pointers: Prints
(nil)for%pwith NULL pointer - Invalid format: Returns -1 if format string ends with
% - Write errors: Returns -1 if write system call fails
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
ft_printf: Main function that parses format string and dispatches to appropriate handlers
ft_printchar: Handles%c- prints a single characterft_printstr: Handles%s- prints a string (handles NULL)ft_printnbr: Handles%dand%i- prints signed integers (handles negative numbers)ft_unsputnbr: Handles%u- prints unsigned integersft_hexaputnbr: Handles%xand%X- prints hexadecimal (lowercase and uppercase)ft_printp/ft_checkp: Handles%p- prints pointer addresses with "0x" prefix
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.
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.