0

I want to write a function which look something like

void put(const char *label, const char *format, ...)
{
    /* There is a file f open for writing */
    fprintf(f, "%s:\n", label);
    fprintf(f, format);
}

... and to use it something like:

put("My Label: ", "A format with number in it %i", 3);

I tried to do it, but I got 16711172 instead of 3 in my file

Silicomancer
  • 8,604
  • 10
  • 63
  • 130
sasha199568
  • 1,143
  • 1
  • 11
  • 33
  • possible duplicate of [Passing variable number of arguments around](http://stackoverflow.com/questions/205529/passing-variable-number-of-arguments-around) – Silicomancer Dec 09 '14 at 17:13

1 Answers1

2

Try the following code:

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

void put(const char *label, const char *format, ...)
{
    va_list         arg_ptr;

    va_start(arg_ptr, format);
    /* There is a file f open for writing */
    fprintf(stdout, "%s: ", label);
    vfprintf(stdout, format, arg_ptr);
    va_end(arg_ptr);
}

int main() {
    put("LAB", "%d\n", 5);
}
Marian
  • 7,402
  • 2
  • 22
  • 34