2
void myPrintf(const char* format, ...) {
    // some code
    va_list vl;
    printf(format, vl);
}

int main() {
    myPrintf("%d\n", 78);
}

In this code I have tried to pass the argument from ellipsis to printf. It compiles but prints garbage instead of 78. What is the right way of doing it?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Ashot
  • 10,807
  • 14
  • 66
  • 117

3 Answers3

7

You need to do the following:

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

Please note the use of vprintf instead of printf.

Abrixas2
  • 3,207
  • 1
  • 20
  • 22
1

Two problems:

  1. The first is that you don't initialize vl, use va_start for that. Don't forget to use va_end afterwards.

  2. The other problem is that printf doesn't take a va_list argument. Use vprintf for that.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

First Initialize vl

va_start(vl,1); //No. of arguments =1

Then take the int from it

printf(format, va_arg(vl,int));

P0W
  • 46,614
  • 9
  • 72
  • 119