In the ft_printf
project, I replicated the functionality of the standard printf function in C.
The main challenge of this project was to learn how variadic functions work and how to use them, which involved handling the formatting and printing of text with different data types. Completing this project made possible adding it to my libft library for future use.
- Reminder: ❗️ You can check all my 42 projects at 42 Cursus
Code Explanation | |
---|---|
ft_printf |
Is the main function that formats and prints the given string with variadic arguments. |
select_format |
A helper function used to select the format of an argument based on the provided value and then call the corresponding formatting function. |
Formatting Functions |
Functions like ft_pf_putchar , ft_pf_putstr , ft_pf_pointer , and ft_pf_putnbr_base are used to formatand print different data types to handle various the different format specifiers |
Format | Description | Formatting Function | Format | putnbr Base |
---|---|---|---|---|
%c | Prints a character | ft_pf_putchar |
int | - |
%s | Prints a string | ft_pf_putstr |
char * | - |
%p | Prints a pointer | ft_pf_pointer ft_pf_putnbr_base |
unsigned long long | hexadecimal |
%d and %i | Prints signed integers | ft_pf_putnbr_base |
int | decimal |
%u | Prints unsigned integers | ft_pf_putnbr_base |
unsigned int | decimal |
%x | Prints integers in lowercase hexadecimal | ft_pf_putnbr_base |
unsigned int | hexadecimal |
%X | Prints integers in uppercase hexadecimal | ft_pf_putnbr_base |
unsigned int | hexadecimal |
%% | Prints a percentage simbol % |
Main example:
int main(void)
{
ft_printf("The answer to %s is %d.\n", "life", 42);
ft_printf("UpTo%c", 'U');
return (0);
}
Output:
The answer to life is 42
UpToU
This implementation does not include all the features and options provided by the standard printf function. It does not handle width, precision, or other formatting flags.