Implementing my own printf in C
int ft_printf(const char *str, ...);
- %c Prints a single character.
- %s Prints a string (as defined by the common C convention).
- %p The void * pointer argument has to be printed 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.
- %% Prints a percent sign
- Declare a va_list ptr
- Iterate through the first string
- If we see % + format_char, we will access the next argument using va_arg and process it according to what the format_char is
ex: as we iterate through the string, we come across %d. The next va_arg is "123". We will use ft_putnbr to print out the argument and count the approriate chars being printed - Otherwise we print the character
- Don't forget to count the characters being printed out!
- If we see % + format_char, we will access the next argument using va_arg and process it according to what the format_char is
- va_end and return count
Variadic functions: functions that can allow a variable number of arguments
- va_list: declare this pointer type to use in the below functions
- va_start: allow access to varaidac function arguments
va_start(va_list ap, argN) va_list is the pointer to the last fixed arg in the variadic function
argN is the last fixed argument n the variadic function - va_arg: access next argument
va_arg(va_list ap, type) type is the type of of data that va_arg should expect - va_copy: make copy of function args
va_copy(va_list dest, va_list src) - va_end: end traversal of function arguments va_end(va_list ap)