Coding

Nested Variadic Functions in C

You may be familiar with variadic functions in C, these are basically functions that allow a variable number of parameters, they are normally written like this:

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

Obviously you can do more with them and they are very useful, but if you want to have one function calling another things can get complicated. This post explores the problem and a couple of ways of solving it.

The core problem is that a va_list can only be used once, if you try to do the following you are in for a bad time:

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

void my_print_func2(bool should_print, const char *format, ...)
{
    if (!should_print)
    {
        return;
    }
    va_list args;
    va_start(args, format);
    my_print_func(format, args);
    va_end(args);
}

This will likely compile, but your text will get quite scrambled along the way when it is executed. So, you have two options, the first is to pass the va_list to the parent function like this:

void my_print_func(const char *format, va_list args)
{
    vprintf(format, args);
}

void my_print_func2(bool should_print, const char *format, ...)
{
    if (!should_print)
    {
        return;
    }
    va_list args;
    va_start(args, format);
    my_print_func(format, args);
    va_end(args);
}

This is fine if you own my_print_func() and the call chain is only going to work like this. But if, like me, you are in a scenario where my_print_func() and my_print_func2() could be called, what can you do? Well, this is where macros come in.

Basically we define my_print_func2() as a macro which calls my_print_func(), the macro can have a small amount of logic if needed. Note that you need to be compiling with at least C99 standard for this to work:

#define my_print_func2(should_print, format, ...)\
    do {\
        if (!should_print)\
        {\
            return;\
        }\
        else\
        {\
            my_print_func(format, __VA_ARGS__);\
        }\
    } while (0)

void my_print_func(const char *format, ...)
{
    va_list args;
    va_start(args, format);
    vprintf(format, args); // Or any function that takes a va_list parameter
    va_end(args);
}

Since the macro essentially fills in code at compile time to where it is used it is essentially just one call to my_print_func(), but it acts like a separate call to the reader.

LinuxJedi

View Comments

  • FWIW, I think line 17 in the last code sample should read:

    my_print_func2(format, args);

    Thank you for this, I learned something new!

    • There is definitely a bug there, thanks for pointing it out. Although it shouldn't call `my_print_func2`, that would fail due to a missing parameter and also get in a loop that would blow the stack. It should have been `vprintf()` or any function that takes a `va_list`. I'll fix it now.

  • Thanks for this artucle Jedi, it truly helped.

    I wish you could elaborate on why this justifies a macro.

    specifically I'm a bit puzzled regarding this quote of yours:

    "This is fine if you own my_print_func() and the call chain is only going to work like this. But if, like me, you are in a scenario where my_print_func() and my_print_func2() could be called, what can you do? Well, this is where macros come in."

    X_X

    Regardless, thank you for this post!

    • I don't remember the exact problem I was trying to solve, it was over 4 years ago. But I believe `my_print_func()` was part of a library I was using and couldn't alter it. The library had a way adding your own `printf()` function, and a callback which was `my_print_func2()`. I think I was trying to turn on/off debug/verbose printing in the library and this was the only way around it.

Share
Published by
LinuxJedi
Tags: COpen Source

Recent Posts

Two special Amiga 4000s: Jops video

I finally got Jops to generate a good DiagROM serial output, but the video output…

7 days ago

Restoration of a barn find Amiga 2000: part 3

All the motherboard issues were resolved in my previous post in this series, now it…

1 week ago

Restoration of a barn find Amiga 2000: part 2

With this Amiga 2000, I previously got it into a state where it would boot…

2 weeks ago

Two special Amiga 4000s: Repairing Jops

Last time I worked on Jops, I left myself a lot of work to do.…

4 weeks ago

Restoration of a barn find Amiga 2000: part 1

I recently acquired an Amiga 2000 for £350 which was in an unknown state, but…

4 weeks ago

Diagnosing an Amiga 1200 Data Path Fault

I recently acquired an Amiga 1200 motherboard in a spares/repairs condition for about £100 recently.…

1 month ago