Dave's Brain

Browse - Programming Tips - How can I make my own custom printf()-like function that accepts

Date: 2014sep26
Language: C/C++
Keywords: snprintf, vsprintf, vprintf, vargs

Q.  How can I make my own custom printf()-like function that accepts
a variable number of arguments?

A.  Use the va_* family of functions like this:

void Log(const char *format, ...) { va_list ap; char buf[5 * 1024]; va_start(ap, format); vsnprintf(buf, sizeof(buf), format, ap); fprintf(fLogHandle, "%s\n", buf); fflush(fLogHandle); va_end(ap); }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.