printf()-like macro in C90

Discussion in 'C Programming' started by pinkfloydhomer@gmail.com, Nov 9, 2005.

  1. Guest

    I want to have debugging macro or function that I can use like:

    DEBUG("bla bla bla %d bla %s bla bla %d %d", 1, "42", 3, 4);

    just as with printf. I want to avoid implementing this functionality
    myself with varargs etc.

    Now, in my case, I cannot use printf etc to output with. I have to use
    some other function that can only take a string. Let's call it
    output(char* str). Today, when debugging, I insert something like:

    {
    char buffer[200];
    sprintf(buffer, "bla bla bla %d bla %s bla bla %d %d", 1, "42", 3,
    4);
    output(buffer);
    }

    This is tedious. I want to just be able to do

    DEBUG("bla bla bla %d bla %s bla bla %d %d", 1, "42", 3, 4);

    or something similar. But I can't just make the above piece of code
    into a macro. I thought about doing something like adding extra
    parentheses like:

    DEBUG(("bla bla bla %d bla %s bla bla %d %d", 1, "42", 3, 4));

    so that the macro receives the entire parenthesis as one argument, but
    then I can't just call sprintf with that argument...

    How do I solve this problem?

    /David
    , Nov 9, 2005
    #1
    1. Advertising

  2. Simon Biber Guest

    wrote:
    > I want to have debugging macro or function that I can use like:
    >
    > DEBUG("bla bla bla %d bla %s bla bla %d %d", 1, "42", 3, 4);
    >
    > just as with printf. I want to avoid implementing this functionality
    > myself with varargs etc.
    >

    ....
    >
    > How do I solve this problem?


    Variable arguments are not that scary. Your function need only be six
    lines long!

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

    void DEBUG(const char *fmt, ...)
    {
    va_list va;
    va_start(va, fmt);
    char buffer[200];
    vsprintf(buffer, fmt, va);
    output(buffer);
    va_end(va);
    }

    All of the hard work was passed off to vsprintf, which is a version of
    the sprintf function that operates on a user-supplied va_list object.
    It's ready-made for situations like this!

    --
    Simon.
    Simon Biber, Nov 9, 2005
    #2
    1. Advertising

  3. Simon Biber Guest

    Simon Biber wrote:
    > wrote:
    >
    >> I want to have debugging macro or function that I can use like:
    >>
    >> DEBUG("bla bla bla %d bla %s bla bla %d %d", 1, "42", 3, 4);
    >>
    >> just as with printf. I want to avoid implementing this functionality
    >> myself with varargs etc.
    >>

    > ...
    >
    >>
    >> How do I solve this problem?

    >
    >
    > Variable arguments are not that scary. Your function need only be six
    > lines long!
    >
    > #include <stdio.h>
    > #include <stdarg.h>
    >
    > void DEBUG(const char *fmt, ...)
    > {
    > va_list va;
    > va_start(va, fmt);
    > char buffer[200];
    > vsprintf(buffer, fmt, va);
    > output(buffer);
    > va_end(va);
    > }


    Replying to myself. Sorry, for C90 you will need to swap the
    va_start(va, fmt); and char buffer[200]; lines above, as all
    declarations must come before other statements.

    --
    Simon.
    Simon Biber, Nov 9, 2005
    #3
  4. Simon Biber wrote:
    > Simon Biber wrote:
    > > wrote:
    > >
    > >> I want to have debugging macro or function that I can use like:
    > >>
    > >> DEBUG("bla bla bla %d bla %s bla bla %d %d", 1, "42", 3, 4);
    > >>
    > >> just as with printf. I want to avoid implementing this functionality
    > >> myself with varargs etc.
    > >>

    > > ...
    > >
    > >>
    > >> How do I solve this problem?

    > >
    > >
    > > Variable arguments are not that scary. Your function need only be six
    > > lines long!
    > >
    > > #include <stdio.h>
    > > #include <stdarg.h>
    > >
    > > void DEBUG(const char *fmt, ...)
    > > {
    > > va_list va;
    > > va_start(va, fmt);
    > > char buffer[200];
    > > vsprintf(buffer, fmt, va);
    > > output(buffer);
    > > va_end(va);
    > > }

    >
    > Replying to myself. Sorry, for C90 you will need to swap the
    > va_start(va, fmt); and char buffer[200]; lines above, as all
    > declarations must come before other statements.
    >
    > --
    > Simon.


    You should use vsnprintf instead if available (in the C99 standard).
    Calling vsnprintf with available size 0 will return the number of
    characters written if n was suffieciently large enough (which you can
    use to allocate the proper amount required, or use the static buffer if
    it's large enough). 7.19.6.12 in C99. You can find implementations of
    vsnprintf on the internet if your C library doesn't have it already.
    Chris Hulbert, Nov 9, 2005
    #4
  5. Jordan Abel Guest

    On 2005-11-09, <> wrote:
    > I want to have debugging macro or function that I can use like:
    >
    > DEBUG("bla bla bla %d bla %s bla bla %d %d", 1, "42", 3, 4);
    >
    > just as with printf. I want to avoid implementing this functionality
    > myself with varargs etc.
    >
    > Now, in my case, I cannot use printf etc to output with. I have to use
    > some other function that can only take a string. Let's call it
    > output(char* str). Today, when debugging, I insert something like:
    >
    > {
    > char buffer[200];
    > sprintf(buffer, "bla bla bla %d bla %s bla bla %d %d", 1, "42", 3,
    > 4);
    > output(buffer);
    >}
    >
    > This is tedious. I want to just be able to do
    >
    > DEBUG("bla bla bla %d bla %s bla bla %d %d", 1, "42", 3, 4);
    >
    > or something similar. But I can't just make the above piece of code
    > into a macro. I thought about doing something like adding extra
    > parentheses like:
    >
    > DEBUG(("bla bla bla %d bla %s bla bla %d %d", 1, "42", 3, 4));
    >
    > so that the macro receives the entire parenthesis as one argument, but
    > then I can't just call sprintf with that argument...
    >
    > How do I solve this problem?
    >
    > /David
    >


    This requires c99.

    #define DEBUG(format,...) do {\
    char *buf; size_t len;\
    len=snprintf(NULL,0,format,__VA_ARGS__);\
    buf=malloc(len+1);\
    if(buf) {\
    snprintf(buf,len+1,format,__VA_ARGS__);\
    output(buf);\
    free(buf);\
    }\
    } while(0)

    writing a function is starting to look attractive, isn't it?
    Jordan Abel, Nov 9, 2005
    #5
  6. pete Guest

    Jordan Abel wrote:

    > This requires c99.
    >
    > #define DEBUG(format,...) do {\
    > char *buf; size_t len;\


    > writing a function is starting to look attractive, isn't it?


    When your macro starts defining objects, then it's
    time to at least give some thought to writing a function.

    --
    pete
    pete, Nov 9, 2005
    #6
  7. Zoran Cutura Guest

    Simon Biber <> wrote:
    > wrote:
    >> I want to have debugging macro or function that I can use like:
    >>
    >> DEBUG("bla bla bla %d bla %s bla bla %d %d", 1, "42", 3, 4);
    >>
    >> just as with printf. I want to avoid implementing this functionality
    >> myself with varargs etc.
    >>

    > ...
    >>
    >> How do I solve this problem?

    >
    > Variable arguments are not that scary. Your function need only be six
    > lines long!
    >
    > #include <stdio.h>
    > #include <stdarg.h>
    >
    > void DEBUG(const char *fmt, ...)
    > {
    > va_list va;
    > va_start(va, fmt);
    > char buffer[200];
    > vsprintf(buffer, fmt, va);
    > output(buffer);
    > va_end(va);
    > }
    >
    > All of the hard work was passed off to vsprintf, which is a version of
    > the sprintf function that operates on a user-supplied va_list object.
    > It's ready-made for situations like this!
    >


    One problem when using a function instead of a macro is, that in that
    function you cannot hide __LINE__ and __FILE__ usage or other macros,
    which is very helpful espesially with DEBUG implementations.

    --
    Z ()
    "LISP is worth learning for the profound enlightenment experience
    you will have when you finally get it; that experience will make you
    a better programmer for the rest of your days." -- Eric S. Raymond
    Zoran Cutura, Nov 14, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ben
    Replies:
    4
    Views:
    610
    Martin Ambuhl
    Jun 26, 2004
  2. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,237
  3. Patrick Kowalzick
    Replies:
    5
    Views:
    469
    Patrick Kowalzick
    Mar 14, 2006
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    430
  5. guru
    Replies:
    8
    Views:
    276
Loading...

Share This Page