By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,486 Members | 1,716 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,486 IT Pros & Developers. It's quick & easy.

printf()-like macro in C90

P: n/a
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 15 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
pi************@gmail.com 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.
Nov 15 '05 #2

P: n/a
Simon Biber wrote:
pi************@gmail.com 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.
Nov 15 '05 #3

P: n/a

Simon Biber wrote:
Simon Biber wrote:
pi************@gmail.com 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.

Nov 15 '05 #4

P: n/a
On 2005-11-09, pi************@gmail.com <pi************@gmail.com> 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?
Nov 15 '05 #5

P: n/a
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
Nov 15 '05 #6

P: n/a
Simon Biber <ne**@ralmin.cc> wrote:
pi************@gmail.com 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 (zo**********@web.de)
"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
Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.