# stdarg

***

**1. Sum of variable arguments**

```c
#include <stdarg.h>

double sum(int count, ...)
{
    double total = 0;
    va_list ap;
    va_start(ap, count);
    for (int i = 0; i < count; i++)
    {
        total += va_arg(ap, double);
    }
    va_end(ap);
    return total;
}

int main()
{
    printf("Sum of 1.2, 3.4, and 5.6: %.2f\n", sum(3, 1.2, 3.4, 5.6));
}
```

**2. Average of variable arguments**

```c
#include <stdarg.h>

double average(int count, ...)
{
    double total = 0;
    va_list ap;
    va_start(ap, count);
    for (int i = 0; i < count; i++)
    {
        total += va_arg(ap, double);
    }
    va_end(ap);
    return total / count;
}

int main()
{
    printf("Average of 1.2, 3.4, and 5.6: %.2f\n", average(3, 1.2, 3.4, 5.6));
}
```

**3. Maximum of variable arguments**

```c
#include <stdarg.h>
#include <limits.h>

double max(int count, ...)
{
    double max = INT_MIN;
    va_list ap;
    va_start(ap, count);
    for (int i = 0; i < count; i++)
    {
        double arg = va_arg(ap, double);
        max = arg > max ? arg : max;
    }
    va_end(ap);
    return max;
}

int main()
{
    printf("Maximum of 1.2, 3.4, and 5.6: %.2f\n", max(3, 1.2, 3.4, 5.6));
}
```

**4. Minimum of variable arguments**

```c
#include <stdarg.h>
#include <limits.h>

double min(int count, ...)
{
    double min = INT_MAX;
    va_list ap;
    va_start(ap, count);
    for (int i = 0; i < count; i++)
    {
        double arg = va_arg(ap, double);
        min = arg < min ? arg : min;
    }
    va_end(ap);
    return min;
}

int main()
{
    printf("Minimum of 1.2, 3.4, and 5.6: %.2f\n", min(3, 1.2, 3.4, 5.6));
}
```

**5. Printf-like function**

```c
#include <stdarg.h>
#include <stdio.h>

void myprintf(const char *format, ...)
{
    va_list ap;
    va_start(ap, format);
    vprintf(format, ap);
    va_end(ap);
}

int main()
{
    myprintf("Hello, %s! Your age is %d.\n", "John", 30);
}
```

**6. Error logging with variable arguments**

```c
#include <stdarg.h>
#include <stdio.h>

void log_error(const char *message, ...)
{
    va_list ap;
    va_start(ap, message);
    vfprintf(stderr, message, ap);
    va_end(ap);
    fprintf(stderr, "\n");
}

int main()
{
    log_error("Error: %s", "File not found");
}
```

**7. Command line argument parsing**

```c
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

int parse_args(int argc, char **argv, ...)
{
    va_list ap;
    va_start(ap, argv);
    int i = 1;
    while (i < argc)
    {
        char *arg = argv[i++];
        if (arg[0] == '-')
        {
            char *option = arg + 1;
            char *value = NULL;
            if (*option == '-')
            {
                option++;
                value = argv[i++];
            }
            va_arg(ap, char *);
            *va_arg(ap, char **) = value;
        }
    }
    va_end(ap);
    return 0;
}

int main(int argc, char **argv)
{
    char *filename = NULL;
    parse_args(argc, argv, "-f", &filename);
    printf("Filename: %s\n", filename);
}
```

**8. Callback function with variable number of arguments**

```c
#include <stdarg.h>
#include <stdio.h>

void callback(void (*func)(void *), void *data, ...)
{
    va_list ap;
    va_start(ap, data);
    func(data);
    va_end(ap);
}

int main()
{
    callback(printf, "Hello, world!\n");
}
```

**9. Function pointer with variable number of arguments**

```c
#include <stdarg.h>
#include <stdio.h>

typedef double (*func_ptr)(double, ...);

double sum_of_squares(double x, ...)
{
    double total = 0;
    va_list ap;
    va_start(ap, x);
    while (x != 0)
    {
        total += x * x;
        x = va_arg(ap, double);
    }
    va_end(ap);
    return total;
}

int main()
{
    func_ptr func = sum_of_squares;
    printf("Sum of squares of 1, 4, and 9: %.2f\n", func(1, 4, 9, 0));
}
```

**10. Macro with variable number of arguments**

```c
#include <stdarg.h>
#include <stdio.h>

#define PRINT_ARGS(...) printf(__VA_ARGS__)

int main()
{
    PRINT_ARGS("Hello, world!\n");
}
```

**11. Passing a variable number of arguments to another function**

```c
#include <stdarg.h>
#include <stdio.h>

void print_args(int count, ...)
{
    va_list ap;
    va_start(ap, count);
    for (int i = 0; i < count; i++)
    {
        printf("%d ", va_arg(ap, int));
    }
    va_end(ap);
    printf("\n");
}

int main()
{
    print_args(3, 1, 2, 3);
}
```

**12. Passing a variable number of arguments to a function pointer**

```c
#include <stdarg.h>
#include <stdio.h>

typedef void (*func_ptr)(int, ...);

void print_args(int count, ...)
{
    va_list ap;
    va_start(ap, count);
    for (int i = 0; i < count; i++)
    {
        printf("%d ", va_arg(ap, int));
    }
    va_end(ap);
    printf("\n");
}

int main()
{
    func_ptr func = print_

```
