How to best cope with variadic input

Jonathan Wakely jwakely.gcc@gmail.com
Tue Mar 1 08:59:00 GMT 2011


On 1 March 2011 05:11,  <richardcavell@mail.com> wrote:
> Hi everyone.  I've decided to learn C99, and so I'm passing -std=c99 to gcc.
>  I've decided to write a Wikipedia bot in C (don't laugh).
>
> Owing partly to obsessive tendencies and partly due to the need for the bot
> to be robust in working unattended at high speed, I want to wrap printf in a
> wrapper that automatically detects when printf has failed and tells the
> user.  The idea is that it is called like this:
>
> int res = safeprint ( __FILE__ , __LINE__ , "Blah" ) ;
> if ( res < 0 ) // uh oh
>
>  But printf has variadic input.  I am scratching my head wondering what is
> my best option:
>
> 1.  Make safeprint a variadic function  (I don't know how to do this)

Use va_list, va_start, va_arg and va_end, defined by:
#include <stdarg.h>

On GNU/Linux you should be able to say "man stdarg.h" to see usage examples.

> 2.  Turn on C++ and overload safeprint with different combinations of
> arguments

A better option would be to use a C++ variadic template.

> 3.  Create multiple safeprint functions with different names, that take
> different combinations of arguments

That would be inconvenient to use.



More information about the Gcc-help mailing list