0

Scenario: I have an array with "n" elements and I wish to pass these n elements as arguments to some function that accepts variable number of arguments.

I am actually trying to write a log function which given an input string and variables, logs them onto a file (STDOUT, say).

Following is the python equivalent of what I want to achieve:

def log(time, num, *arg):
    print(time, *arg)
    return

log("19:00", 2, 3, "abc")

Following is my attempt to do the same in C

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

void log(const char* time, int num, ...)
{
    // num is the number of arguments
    va_list valist;
    va_start(valist, num);

    // what do i do next?
}

int main()
{
    log("19:00", 2, 3, "abc");
}

Output should be:

19:00 3 abc

It would be better if someone can suggest a method in which I could simply pass formatted string and arguments to log function in C like:

int d = 3;
char s[4] = "abc\0";
log("%s %d %s", "19:00", d, s);

0 Answers0