1

I am working on making a version of printf in an embedded system that will print over a variety of interfaces. I am using snprintf to create the string and then sending it out over the interface. My curiosity is what the "..." being passed into the function is. The printf that I am using looks like, this is taken from an embedded printf library

void u_printf(const char* format, ...)

In this case the ... is some number of assorted variables passed into the function. But if I want to write my own version of this how do I get those variables passed in so I can then pass them on to snprintf.

void u_printf(const char* format, ...){
    int cx;
    char buffer[100];
    cx = x_snprintf(buffer, 100, format, ??);
    // function to send buffer to interface goes here    
}

So how do I go about getting the ... variables passed in to where the ?? mark is? Thanks!

Some quick help revealed that va_list is the answer here. When the ... is present we can grab the arguments passed in by creating va_list va variable. This can then be used or passed to vsnprintf()

Meozaa
  • 95
  • 9
  • 2
    Look up *variadic functions* – Eugene Sh. Oct 25 '18 at 13:32
  • 3
    If you provide C-ellipsis version, provide also version with [`va_list`](https://en.cppreference.com/w/cpp/utility/variadic/va_list) (as [vprintf](https://en.cppreference.com/w/cpp/io/c/vfprintf)). – Jarod42 Oct 25 '18 at 13:32
  • 1
    Search for `va_list` on the duplicate's web page – StoryTeller - Unslander Monica Oct 25 '18 at 13:33
  • 1
    @StoryTeller I somewhat disagree with the question you chose as a duplicate. I think https://stackoverflow.com/questions/2735587/in-a-c-function-declaration-what-does-as-the-last-parameter-do is much closer to what is asked here. Though there is an answer to this question in the linked one. – Dan M. Oct 25 '18 at 13:34
  • va_list it is. I started looking into it right after I posted. I guess posting the question helped me to summarize my problem. Thanks for the help! – Meozaa Oct 25 '18 at 13:34

0 Answers0