-1

Say I want to implement a function

void myprintf(const char* format, ...){
   printf("Hello world!\n"),
   printf(format, ...);
}

I.e. I want to pass along the varargs list to printf. Is there any convenient way to do this?

embedded_crysis
  • 193
  • 2
  • 12
  • @embedded_crysis Please, google "c varargs". These answers I have found (among tons of others): [SO: An example of use of varargs in C](http://stackoverflow.com/questions/15784729/an-example-of-use-of-varargs-in-c), [SO: Passing variable number of arguments around](http://stackoverflow.com/questions/205529/passing-variable-number-of-arguments-around). – Scheff's Cat Feb 16 '17 at 09:02

2 Answers2

3

No. However the library functions in the printf family offer a vprintf varaint that accepts a va_list as an argument instead of ellipsis. And it is in fact a good practice to offer such a variant if you happen to be writing your own variable argument function.

Your wrapper would then be something like this:

void myprintf(const char* format, ...){
   printf("Hello world!\n"),
   va_list args;
   va_start(args, format);
   vprintf(format, args);
   va_end(args);
}
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
2

With a little help of the preprocessor:

#include <stdio.h>

#define myprintf(...) \
do { \
   printf("Hello world!\n"), \
   printf(__VA_ARGS__); \
} while (0)

int main(void)
{    
    myprintf("%s %d\n", "Hello", 1);
    return 0;
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94