#💾/42/Core_Curriculum/Ft_printf
- « [[42Core_Projects]]
- ==This project is pretty straightforward, It's goal is to recode printf.==
- I am very proud of this project because I made it in only one file.
%cPrints a single character.%sPrints a string (as defined by the common C convention).%pThe void * pointer argument has to be printed in hexadecimal format.%dPrints a decimal (base 10) number.%iPrints an integer in base 10.%uPrints an unsigned decimal (base 10) number.%xPrints a number in hexadecimal (base 16) lowercase format.%XPrints a number in hexadecimal (base 16) uppercase format.%%Prints a percent sign.
Variadic functions are functions that can accept an indefinite number of arguments. In other words, they can take any number of arguments that are passed to them during the function call.
#include <stdarg.h>
void print_ints(int count, ...)
{
int i = 0;
int total = 0;
va_list args;
va_start(args, count);
while ( i < count)
{
total += va_arg(args, int);
printf("%d: %d\n",i ,value);
i++;
}
va_end(args);
return total;
}
int main()
{
print_ints(3, 24, 26, 311);
print_ints(2,256,51);
}In this example, the
print_intsfunction takes a variable number of arguments, where the first argumentcountspecifies how many integers will be passed. The function uses thestdarg.hlibrary to handle the variable argument list, which is accessed using theva_list,va_start,va_arg, andva_endmacros. Variadic functions can be very useful when you need to write a function that can accept a varying number of arguments, such as a formatting function that prints out a string with dynamically determined values, or a function that needs to perform a calculation on a variable number of values.
This macro defines a type that is used to declare a variable argument list. For example,
va_list my_args;declares a variablemy_argsthat can hold an argument list.
This macro initializes the argument list. It takes two arguments: the first is the
va_listobject, and the second is the last known named argument before the ellipsis. For example,va_start(my_args, count)initializes the argument listmy_argsand sets the starting point forva_argto the argument aftercount.
This macro retrieves the next argument from the argument list. It takes two arguments: the first is the
va_listobject, and the second is the type of the argument being retrieved. For example,va_arg(my_args, int)retrieves the next argument inmy_argsas anint.
This macro cleans up the argument list after it has been used. It takes one argument, which is the
va_listobject being cleaned up. For example,va_end(my_args)cleans up themy_argsargument list.