2

I want to develop a basic function which includes the printf() one. Having troubles with the arguments of the function

I'm trying to develop a basic function similar to this one:

basi_fun(const char* CMD, val1, val2, val3)
{
     fun1();
     printf(CMD,val1,val2?,...);
     fun2();
}

The problem is related to the fact that I don't know how printf() is able to understand the number and type of variable needed. In a similar way I don't know how to allow my basic_fun() to ask for a variable number and type of parameter. By looking in the stdio.h header I understood that the function printf() is based on vfprintf(...):

extern int  vfprintf(FILE *__stream, const char *__fmt, va_list __ap);

and by means of __ap is able to accept a variable number of variables which number and types is defined by the number and type of %d,%c,etc inside the __fmt char array. After this I tried to develop something similar to this:

basi_fun(const char *__fmt, va_list __ap)
{
     fun1();
     printf(__fmt, __ap);
     fun2();
}

Actually it still do not work. What am I missing? Also an indication on where to learn the __ap could be useful.

Cid
  • 14,968
  • 4
  • 30
  • 45
  • 1
    You probably want to read [this](http://www.cplusplus.com/reference/cstdio/vprintf/) – Jabberwocky Nov 29 '19 at 08:25
  • 1
    Does https://stackoverflow.com/questions/150543/forward-an-invocation-of-a-variadic-function-in-c help? – Ry- Nov 29 '19 at 08:28
  • 1
    I edit the question. You were both really fast and helpfull to me. I maneged to solve my problem in just 5 min thanks to you! I know it was a very stupid question but i was stack. – cesarbonetz Nov 29 '19 at 08:31

0 Answers0