The
Each character is written as output until we reach '%'. When the function finds any format specifier, looks for it and output the same string with the new length of the formatted string. This is possible thanks to the use of variadics functions.
printf
function is a command to display a formatted output string on the standart output. 'Formatted' means that format specifiers, which begin with the % character, indicate the location and method of converting a data element (such as a number) into characters.Each character is written as output until we reach '%'. When the function finds any format specifier, looks for it and output the same string with the new length of the formatted string. This is possible thanks to the use of variadics functions.
Allowed functions :
write
, va_start
, va_arg
, va_list
, va_end
.How to ... Printf
You must include first " stdio.h " library.
#include <stdio.h>
int main()
{
printf("hi, my name is %s, and I'm %i", "Eduardo", 25);
return ;
}
Here we are passing 29 bytes characters in the first string, the output will be 34. Printf type is int , we are counting everytime how many bytes exist.
Character | Description |
---|---|
% | Prints a '%' character. |
%c | Prints a character (char). |
%s | Prints a null-terminated '\0' string ("str"). |
%p | Prints the address of a pointer or any other variable. The output is displayed in hexadecimal value. It's a format specifier which is used if we want to print data of type (void *). |
%i, %d | Prints an int as a signed integer. %d and %i are synonymous for output (int). |
%u | Prints decimal unsigned int. |
%x, %X | Prints an unsigned int as a hexadecimal number. x uses lower-case and X uses upper-case letters. |
Variadics functions are functions that can receive a variable number of arguments.
int ft_printf(char str, ...)
In this case our ft_printf receive a string as a first argument, and this (...) means that the function has variadic arguments.
- va_start : Macro used to initialize a 'va_list' object to start accessing the additional arguments from the last fixed argument received by the function. It must be called before using 'va_arg'.
- va_list : Is a data type used to access the additional arguments of a variadic function. A variable of this type is declared in the function that will receive the variable arguments.
- va_arg : Macro used to access each additional argument. It takes the 'va_list' object and the data type of the next argument. Each call to 'va_arg' retrieves the next argument in the list.
- va_end : Macro used to clean up the 'va_list' object when it is no longer needed.
Printf Functions | Description |
---|---|
ft_funlibft_pf | Edited functions from libft. |
ft_printf | Recreates printf function. |
ft_strlen | Returns the total lenght of the string. |
ft_fillformat_pf | Chooses what type of variable is going to print. |
*ft_funlibft_pf functions | Description |
---|---|
ft_putchar_pf | Prints character and increments count . |
ft_putnbr_pf | Prints numbers in decimal base. It puts too numbers in hexadecimal base. |
ft_putstr_pf | Prints a string. |
ft_putptr_pf | Prints a pointer in hexadecimal base, adding "0x" before. |