Variadic Functions and the Ellipsis ( ) ... In C, the ellipsis ( ) is used in function signatures to indicate that the function can accept an arbitrary number of arguments. ... int ft_printf(const char *str, ...) The arguments passed to the function using this mechanism are called “variable arguments(va)”. What is ? va_list At its core, is a way for C functions to accept a variable number of arguments. It's like a special kind of list that holds all the extra arguments you pass to a function. va_list The Magical Macros There are a few key macros that come into play when using : va_list : Think of this as "starting the list". It initialises the list to point to the first variable argument. va_start : This is how you get the next argument from the list. va_arg : This "ends the list" and cleans things up. va_end : This is for copying the list. va_copy All these functions are found in the library. stdarg.h A Simple Example: Mini-printf Let’s create a mini version of that only supports (for integers) and (for strings). It'll showcase in action. printf %d %s va_list #include <stdarg.h> #include <stdio.h> void mini_printf(const char *format, ...) { va_list args; // Declare a va_list variable to manage the variable arguments // Initialize the va_list 'args' to start at the argument after 'format' va_start(args, format); while (*format) // Loop through the format string { // If a format specifier is encountered if (*format == '%') { format++; if (*format == 'd') { // Fetch the next argument as an integer and print it printf("%d", va_arg(args, int)); } else if (*format == 's') { // Fetch the next argument as a string and print it printf("%s", va_arg(args, char *)); } } else { // Print regular characters putchar(*format); } format++; // Move to the next character } // Cleanup the va_list 'args' after processing va_end(args); } int main(void) { mini_printf("Hello %s, number is %d\n", "World", 42); return (0); } Notice how is used after the fixed argument ( ), and retrieves the arguments based on the format specifiers. va_start format va_arg Lets dive more into each of the parameters 1. : Initialising the Argument List va_start What it does: is the starting point. It initialises a to point to the first va_start va_list of the variable arguments. How to use it: It requires two arguments: A variable va_list The last named argument before the variable arguments Example: va_start(args, format); Here, is the variable, and is the last named argument of our function. args va_list format Behind the scenes: When you call a function, the arguments are typically placed onto a stack. sets up the to point to the first variable argument on this stack. va_start va_list 2. : Retrieving Arguments va_arg What it does: fetches the next argument in the list. It moves the pointer forward by the size of the type specified. va_arg How to use it: It requires two arguments: The variable va_list The desired type of the next argument Example: if (*format == 'd') { // Fetch the next argument as an integer and print it printf("%d", va_arg(args, int)); } This will print the next argument as an integer. Behind the scenes: The macro navigates through the stack, fetching arguments based on the size of the specified type. It’s essential to specify the correct type; otherwise, you might retrieve garbage values or cause undefined behavior. 3. : Cleaning Up va_end What it does: is the counterpart to . It cleans up the memory associated with the . va_end va_start va_list How Does It Work? Without diving too deep into the weeds, here’s a simple explanation: When you call a function, the arguments you pass in are typically placed onto a “stack” (think of it like a stack of books). With , we're essentially navigating through this stack to fetch the arguments one by one. va_list Important Notes Before the variable arguments, you should have at least one named argument. This is essential for to work. Always have at least one fixed argument: va_start Functions using don't inherently know the type and number of variable arguments. This is why functions like need format specifiers. The type and number of arguments need clarity: va_list printf can be tricky. If not used correctly, it can lead to undefined behavior. Always ensure that you process the right number and type of arguments. Safety: va_list Links to follow me: LinkedIn GitHub Instagram Also published . here