TUTORIAL: Writing a Custom printf() Wrapper Function in C

  • spork
  • Brewmaster
  • Silver Member
  • spork
  • Posts: 6299
  • Loc: Seattle, WA

Post 3+ Months Ago

Note: The complete source code for the examples in this tutorial can be downloaded at the end of the tutorial.


The function printf() and its cousins are among of the most versatile and well-known functions for sending formatted strings as output to files and the screen. They are implemented by many languages, including C, Java, Perl, PHP, Python, Ruby, and a handful of others.

One of printf()'s signature benefits is its ability to accept a variable number of arguments. This allows printed text to contain countless variations of formatted values and strings. In this tutorial, you will learn how to extend this functionality to create your own printf() wrapper function.

Why would we want to do this? One of the most obvious reasons would be to create our own warning() or error() functions that act just like printf() but can be conditionally switched on or off by, say, compiler or command line arguments. This would save us the need to wrap individual debug statements in conditional blocks, cutting down code size and complexity.

This tutorial will demonstrate how to do this in C.

Our Silver Bullet: vprintf()

The secret to our success here is printf()'s close cousin, vprintf(). This function does the same thing as printf(), but differs in the way it accepts arguments. As we know, printf() accepts a format string and then a variable number of arguments. vprintf() on the other hand accepts a format string and a pointer to a list of arguments. It is this second argument that allows us to easily pass all of the arguments from our function into printf().

In C, we must include an additional header file to accomplish this: stdarg.h. This file contains the va_list type, which is essentially a pointer to our list of args. It also contains the functions va_start() and va_end(), which provide us access to these arguments. Below is an example function in C that takes the same arguments as printf() and simply passes them on to vprintf().
C Code: [ Select ]
#include <stdio.h>
#include <stdarg.h>
void printThis( const char* format, ... ) {
    va_list args;
    va_start( args, format );
    vprintf( stderr, format, args );
    va_end( args );
  1. #include <stdio.h>
  2. #include <stdarg.h>
  4. void printThis( const char* format, ... ) {
  5.     va_list args;
  6.     va_start( args, format );
  7.     vprintf( stderr, format, args );
  8.     va_end( args );
  9. }

To specify a variable number of arguments in a function, the "..." keyword is used. This tells the compiler that any number of arguments may follow the format parameter.

Putting It All Together

Now that we've successfully wrapped printf() in our own function, let's go ahead and create our own error() function that behaves exactly like printf(), but prints to standard error and displays "Error: " before our messages. Just like with fprintf(), we can use vfprintf() to specify the output file (in this case, stderr) and print to it using the specified format string and arguments.
C Code: [ Select ]
#include <stdio.h>
#include <stdarg.h>
void error( const char* format, ... ) {
    va_list args;
    fprintf( stderr, "Error: " );
    va_start( args, format );
    vfprintf( stderr, format, args );
    va_end( args );
    fprintf( stderr, "\n" );
  1. #include <stdio.h>
  2. #include <stdarg.h>
  4. void error( const char* format, ... ) {
  5.     va_list args;
  6.     fprintf( stderr, "Error: " );
  7.     va_start( args, format );
  8.     vfprintf( stderr, format, args );
  9.     va_end( args );
  10.     fprintf( stderr, "\n" );
  11. }

That's all there is to it. You can call error() the same exact way you would call printf(). As I mentioned earlier, you can surround its definition with a conditional so that output will only be generated if a given flag is set. Useful, eh?


You should now know how to create your own wrapper function for printf() in order to customize it to suit your needs. Keep in mind that you can create functions with variable-length arguments for many other purposes as well.

You may find the following resources helpful when working with the printf() family of functions:
printf(), fprintf(), vprintf(), vfprintf()

I always welcome questions or feedback about this tutorial. Simply post a reply or PM me; I'm glad to help!

(608 Bytes) Downloaded 2489 times

Complete source code for the example in this tutorial.

Post Information

  • Total Posts in this topic: 1 post
  • Moderator: Tutorial Writers
  • Users browsing this forum: No registered users and 2 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum

© 1998-2016. Ozzu® is a registered trademark of Unmelted, LLC.