Recreate a basic version of the function printf()
. The following conversion must be recognized cspdiuxX%
.
PS: As printf()
is an int function, we have to return the count of each characters that we write.
A function may be called with a varying number of arguments of varying types. The called function must declare an object of type va_list
which is used by the macros va_start()
, va_arg()
, and va_end()
.
va_list
will store all the argument passed to the function int ft_printf(const char *input, ...)
. As we can see, the ...
is telling the function that it will receive variadic arguments.
Now we have setup this, we need to call the arguments, to do so va_start(args, input)
is here for us.
As ft_printf()
writes a string after converting all the arguments passed to it, we have to simplie check if the current character is a %
.
Case 1
: If the character is not a%
we simply do a write of the characterCase 2
: We found a%
, then we check what current char +1 to have the variadic argument and pass it to the functionvoid ft_printf_args(char convert, va_list args, int *rcount)
.
This argument will simply write a character. We call the function ft_putchar_fd()
and add +1 to the return *rcount
.
Prototype: int ft_printf_char(int c, int *rcount)
This arguments will write a string of characters. We check if the string passed to it isn't empty.
NULL
: callft_pustr_fd()
and pass (null) to write that the current string is equal to nothingValid string
: callft_pustr_fd()
to write the string
Both of the condition will count the number of characters returned with ft_strlen()
and save the result to the *rcount
.
Prototype: int ft_printf_str(char *str, int *rcount)
This argument will print the address of a variable in hexadecimal format. To do so we convert a void *ptr
to an unsigned long
and convert it in hexadecimal format with the function ft_printf_hex()
.
Prototype: int ft_printf_ptr(void *ptr, int *rcount)
Both of this arguments are returning a decimal numbers in base 10
. We simply use an ft_putnbr()
and then print the value.
Prototype: int ft_printf_int(int nbr, int *rcount)
This arguments will return a data values from zero to positive numbers. An ft_putnbr()
modified with unsigned int nbr
is requested as argument.
Prototype: int ft_printf_dun(unsigned int nbr, int *rcount)
This argument will convert any number to his hexadecimal value.
Hexadecimal numbers uses 16 values to represent a number.
Numbers from 0-9 are expressed by digits 0-9
and 10-15
are represented by characters from A – F
.
To do so we use the same concept as ft_putnbr()
:
- Divide the number by 16
- Check if the rest is less than 10.
- If it is, then add 48 to the rest and store the result in the array hex.
- Otherwise, add 87 to the rest and store the result in the array hex.
The difference between %x
and %X
is only represended by a - f
and A - F
.
Prototype: int ft_printf_hex(unsigned long nbr, int *rcount, int format)
This argument will write a %
.