sasha199568
sasha199568

Reputation: 1153

How to nest fprintf in another function

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

Upvotes: 0

Views: 51

Answers (1)

Marian
Marian

Reputation: 7482

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);
}

Upvotes: 2

Related Questions