Statement of the project (in Spanish / English)
This project aims to recreate the functionality of the printf
function from the C standard library (<stdio.h>
). The goal is to implement this function using variadic arguments, providing support for various format specifiers and handling different types of data.
-
Understand and implement variadic functions in C.
-
Handle different format specifiers to print various data types.
-
Manage multiple flags and their combinations for formatting output.
Variadic functions are functions that accept a variable number of arguments. In C, this is achieved using the stdarg.h
library, which provides macros to handle the arguments.
-
va_list
: A type to hold the information needed to retrieve the additional arguments. -
va_start
: Initializes ava_list
variable to retrieve the additional arguments. -
va_arg
: Retrieves the next argument in the list. -
va_end
: Cleans up theva_list
variable.
The prototype of the function is the following:
int ft_printf(const char *str, ...);
The ft_printf
function must handle the following format specifiers:
Specifier | Description |
---|---|
'%c' | Prints a single character |
'%s' | Prints a string |
'%p' | Prints a pointer in hexadecimal format |
'%d' | Prints a decimal (base 10) number |
'%i' | Prints an integer in base 10 |
'%u' | Prints an unsigned decimal (base 10) number |
'%x' | Prints a number in hexadecimal (base 16) lowercase format |
'%X' | Prints a number in hexadecimal (base 16) uppercase format |
'%c' | Prints a single character |
'%%' | Prints a percent sign |
The bonus part extends the functionality to handle various flags for formatting:
Flags | Description |
---|---|
'-' | Right-padding with spaces |
'0' | Left-padding with zeroes |
'.' | Indicates precision |
'#' | Adds "0x" to a hexadecimal number |
' ' | Adds a space before a positive number |
'+' | Adds a sign (+/-) before a number |
The general format for the ft_printf
function with flags is:
%[flags][width][.precision]type
-
Parsing the Format String: Identify and extract format specifiers and flags from the input string.
-
Handling Variadic Arguments: Use va_list to manage a variable number of arguments passed to the function.
-
Implementing Specifiers: Write functions to handle each format specifier, converting the arguments to their respective string representations.
-
Managing Flags: Implement the logic to handle different flags and apply them to the formatted output.
-
Combining Results: Construct the final output string by combining the processed arguments and print it.