0

I would like to modify the function printf to a new function printf2 that simply prepends the message to be printed with Hello.

I could do

void printf2(char message[]) {
    printf("Hello ");
    printf(message);
}

The problem is that I cannot pass the extra arguments for cases when message has %d, %c, etc.

How can I have printf2 accept as many parameters printf can, and pass them on to printf?

Randomblue
  • 112,777
  • 145
  • 353
  • 547
  • 4
    Have a look at `vprintf`: http://www.cplusplus.com/reference/clibrary/cstdio/vprintf/ – display101 Jul 19 '12 at 15:33
  • 1
    possible duplicate of [call printf using va_list](http://stackoverflow.com/questions/5977326/call-printf-using-va-list) – Oliver Charlesworth Jul 19 '12 at 15:48
  • @KeithHalligan: I'm on an embedded device, and I don't have `vprintf`. – Randomblue Jul 19 '12 at 16:05
  • Are you SURE you don't have vprintf()? Perhaps you just didn't #include and assumed the resulting error meant you don't have vprintf()? What sort of embedded device are you using, and what compiler? – phonetagger Jul 19 '12 at 17:08
  • @Randomblue - please provide some details regarding your embedded dev environment (compiler, C99, etc). – Throwback1986 Jul 19 '12 at 18:25
  • I'm developing for an STM32F2 ARM chip using the Yagarto toolchain (recompiled version of GCC). For licensing reasons, I cannot use the standard libraries. I have however found a `printf` function with a compatible license, but that is it. – Randomblue Jul 19 '12 at 18:27
  • Licensing issues fundamentally change the nature of your question. I assume that the usual list isn't compatible with your project (GPL, BSD, etc?) – Throwback1986 Jul 20 '12 at 13:50

3 Answers3

2

The comment above points you in the right direction, but here is an example of how to prepend your tag (Hello).

Notes: I have used the s and n version of printf to format a new string that shouldn't overflow my temp buffer, and *MAX_MSG_SIZE* is assumed to be defined appropriately elsewhere.

void printf2(const char *format, ...)
{

    char buffer[MAX_MSG_SIZE] = "";  


    va_list args;


    va_start(args,format);
    vsnprintf(buffer, MAX_MSG_SIZE, format, args);
    va_end(args);   

    printf("Hello: %s\n", buffer);
}
Throwback1986
  • 5,887
  • 1
  • 30
  • 22
  • 1
    It doesn't appear that a single call to `printf` is required; so the printing to string (with its associated MAX_MSG_SIZE limit) isn't necessary. Move the `printf("Hello: ");` above va_start, and replace `vsnprintf()` with `printf(format, args);` – Kevin Vermeer Jul 19 '12 at 16:03
  • The print to string was necessary in the snippet my example was extracted from - but you are correct in that there are a couple of ways this can be accomplished. – Throwback1986 Jul 19 '12 at 18:12
  • Replacing `vsnprintf()` with `printf(format, args);` does not work. Replacing it with `vprintf(format, args);` does. – Urhixidur Mar 12 '15 at 14:34
0
  1. You should use below function to control the variable parameter.

    void va_start( va_list arg_ptr, prev_param );   
    type va_arg( va_list arg_ptr, type );   
    void va_end( va_list arg_ptr );  
    
  2. Judge the format string. using switch () statement judge %d, %c, %s and so on

Simon Dugré
  • 17,980
  • 11
  • 57
  • 73
enyblock
  • 1
  • 1
0
void print_message(char *format, ...)
{
    printf("%s", "Hello: ")
    va_list ptr;
    va_start(ptr, format);
    vprintf(format, ptr);
    va_end(ptr);
}

Note:

  1. printf("%s", "Hello: ") is bit faster than printf("Hello: ") (by skipping scan of the format string phase)
  2. printf(message); is REALLY bad idea. It crash when message has something like "%s".
Dmitry Poroh
  • 3,705
  • 20
  • 34