4

I want to only printf if some condition is true. I know printf is a variadic function but sadly I can't seem to find any thread here explaining I can wrap it.

Basically every in the code where I'd write :

printf(" [text and format] ", ... args ...);

I want to write something like

my_custom_printf(" [text and format] ", ... args ...);

Which then is implemented like this :

int my_custom_printf(const char* text_and_format, ... args ...)
{
    if(some_condition)
    {
        printf(text_and_format, ... args...);
    }
}

A first version of the condition would be independent of the args (it would be on some global variable), but it might be in the future that it's a condition on argument that's wanted.

Anyway, right now I just need the syntax for ... args ... in the prototype and the body of my_custom_printf.

I'm using GCC but I don't know which C standard - but we can just try stuff out.

Charles
  • 988
  • 1
  • 11
  • 28
  • 3
    The `v*printf` family of functions from `` does that, [see here](https://stackoverflow.com/questions/1485805/whats-the-difference-between-the-printf-and-vprintf-function-families-and-when). – M Oehm Apr 29 '19 at 12:17
  • 3
    Possible duplicate of [what's the difference between the printf and vprintf function families, and when should I use one over the other?](https://stackoverflow.com/questions/1485805/whats-the-difference-between-the-printf-and-vprintf-function-families-and-when) – mch Apr 29 '19 at 12:17
  • Didn't know about `vprintf` - but now I do and I can do what I want. Thanks ! – Charles Apr 29 '19 at 12:20
  • Possible duplicate of [Pass varargs to printf](https://stackoverflow.com/questions/21540778/pass-varargs-to-printf) – phuclv Apr 29 '19 at 15:25
  • [How to pass variable number of arguments to printf/sprintf](https://stackoverflow.com/q/1056411/995714), [How to wrap a function with variable length arguments?](https://stackoverflow.com/q/41400/995714), [call printf using va_list](https://stackoverflow.com/q/5977326/995714), [Forward an invocation of a variadic function in C](https://stackoverflow.com/q/150543/995714) – phuclv Apr 29 '19 at 15:26
  • 1
    Does this answer your question? [How to pass variable number of arguments to printf/sprintf](https://stackoverflow.com/questions/1056411/how-to-pass-variable-number-of-arguments-to-printf-sprintf) – phuclv Apr 11 '22 at 09:11
  • Sure, why not. But my question has already been answered three years ago. – Charles Apr 11 '22 at 09:14
  • @Charles that's an auto-generated comment from SO, no one asks such silly question. Please click on the message on top from SO to close this as a duplicate of the other question – phuclv Apr 17 '22 at 09:26

1 Answers1

8

You can use vprintf:

#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>

static bool canPrint = true;

int myprintf(const char *fmt, ...)
{
    va_list ap;
    int res = 0;

    if (canPrint) {
        va_start(ap, fmt);
        res = vprintf(fmt, ap);
        va_end(ap);
    }
    return res;
}

int main(void)
{
    myprintf("%d %s\n", 1, "Hello");
    return 0;
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94