How to wrap printf() into a function or macro?

There are 2 ways to do this:

  1. Variadric macro

    #define my_printf(...) printf(__VA_ARGS__)
  2. function that forwards va_args

    #include <stdarg.h>
    #include <stdio.h>
    void my_printf(const char *fmt, ...) {
        va_list args;
        va_start(args, fmt);
        vprintf(fmt, args);

There are also vsnprintf, vfprintf and whatever you can think of in stdio.

Leave a Comment