473,326 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

printf()-like macro in C90

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
6 6496
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
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Grumble | last post by:
Hello, I have the following structure: struct foo { char *format; /* format string to be used with printf() */ int nparm; /* number of %d specifiers in the format string */ /* 0 <= nparm <=...
8
by: aditya | last post by:
hi, Can anybody please tell me that how the following printf(...) statement works- main(){ int d=9; printf("%d",printf("%d")); return 0;
9
by: Eric Lilja | last post by:
Hello, I have two code snippets I want you to look at. My program compiles without warnings (warning level set to max, gcc 3.4.3) with either snippet but the latter one causes a segfault at...
29
by: whatluo | last post by:
Hi, c.l.cs I noticed that someone like add (void) before the printf call, like: (void) printf("Timeout\n"); while the others does't. So can someone tell me whether there any gains in adding...
34
by: Old Wolf | last post by:
Is there any possible situation for printf where %hd causes a different result to %d, and the corresponding argument was of type 'short int' ?
5
by: Sanjay Kulkarni | last post by:
I am facing some problems while trying to explore the printf statement: Expected Actual Result Statement Result 1 printf("x = %d", 18.0 / 14); 18725 5.0 printf("\nx = %f",...
6
by: MikeC | last post by:
Folks, Can the width specifier be used in a printf() text string? If I execute... printf("%4s", ".........."); it prints ten dots. If I execute
11
by: Googy | last post by:
Hi friends!! As we know that the input parameters in a function is fixed when the function is defined but how does printf processes variable number of input arguments ? For example: 1....
3
by: google | last post by:
Consider the following code: char str; char str2; strcpy(str, "%alfa% %beta% d%100%d %gamma% %delta%"); printf("printf: "); printf("1%s2", str); printf("\nsprintf: "); sprintf(str2, "1%s2",...
10
by: Rahul | last post by:
Hi Everyone, I had a query reg printf(). I heard that it can't be used in ISR's as it is non-re-enterant. I didn't get as to why printf is non-re-enterant and why non-re-enterant library can't...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.