473,320 Members | 1,823 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,320 software developers and data experts.

Variable list in pre-processor define

Is there a way to make a pre-processor define with a variable number of
arguements? I want to have something like this:

#define DBG_WRITE(char *A, ...)
{
va_list vl;
char dbgTmpStr[1024];
va_start(vl, A);
vsprintf(dbgTmpStr, A, vl);
va_end(vl);
fprintf(gDbgFP, dbgTmpStr);
fflush(gDbgFP);
}

But this obviously doesnt compile.
At the moment I have DBG_WRITE0 for no additional params, DB_WRITE1 for 1
additional param and so on which is quite messy.
I can make a define that works for printf like this:

#define DBG_PRINTF(A) printf A;
and then I just call it like this: DBG_PRINTF(("Hello %s\n, someName));
(note the double brackets).

Is there a way I can achieve this for fprintf()?

Thanks,
Allan
Nov 14 '05 #1
4 2043


Allan Bruce wrote:
Is there a way to make a pre-processor define with a variable number of
arguements? I want to have something like this:

#define DBG_WRITE(char *A, ...) ^^^^^^
You want to have a macro, not a function. {
va_list vl;
char dbgTmpStr[1024];
va_start(vl, A);
vsprintf(dbgTmpStr, A, vl);
va_end(vl);
fprintf(gDbgFP, dbgTmpStr);
fflush(gDbgFP);
}

But this obviously doesnt compile.
At the moment I have DBG_WRITE0 for no additional params, DB_WRITE1 for 1
additional param and so on which is quite messy.
I can make a define that works for printf like this:

#define DBG_PRINTF(A) printf A;
and then I just call it like this: DBG_PRINTF(("Hello %s\n, someName));
(note the double brackets).

Is there a way I can achieve this for fprintf()?


In C99: Yes.
#define DBG_WRITE(fmt, ...) \
{\
fprintf(gDbgFP, fmt, __VA_ARGS__);\
fflush(gDbgFP);\
}

For function like macros, one usually does
#define DBG_WRITE(fmt, ...) \
do {\
fprintf(gDbgFP, fmt, __VA_ARGS__);\
fflush(gDbgFP);\
} while (0)
which works with
if(....)
DBG_WRITE("Failure (line %d)\n",__LINE__);
else
......
(see clc FAQ)

For C89, I suggest writing a vararg function and then
calling v*printf().
Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #2
In message <11*************@corp.supernews.com>
"Allan Bruce" <ab****@TAKEMEAWAY.csd.abdn.ac.uk> wrote:
Is there a way to make a pre-processor define with a variable number of
arguements? I want to have something like this:

#define DBG_WRITE(char *A, ...)
{
va_list vl;
char dbgTmpStr[1024];
va_start(vl, A);
vsprintf(dbgTmpStr, A, vl);
va_end(vl);
fprintf(gDbgFP, dbgTmpStr);
fflush(gDbgFP);
}

But this obviously doesnt compile.
At the moment I have DBG_WRITE0 for no additional params, DB_WRITE1 for 1
additional param and so on which is quite messy.
I can make a define that works for printf like this:

#define DBG_PRINTF(A) printf A;
and then I just call it like this: DBG_PRINTF(("Hello %s\n, someName));
(note the double brackets).

Is there a way I can achieve this for fprintf()?


C99 adds the feature of variadic macro arguments. Even if not fully
C99-conformant, a lot of compilers will probably support at least this
feature, as it's pretty easy:

#define DBG_WRITE(...) \
do { \
fprintf(gDbgFP, __VA_ARGS__); \
fflush(gDbgFP); \
} while (0)

The trailing macro parameter(s) that go in the position of the ellipsis are
substituted into __VA_ARGS__. Unlike functions, it's legal to have the
ellipsis and no named parameters.

By the way, rather than have the fflush after every fprintf, it might be
neater to change the buffering of gDbgFP to be line-based, or make it
non-buffered, with a call to setvbuf.

I also don't understand what the temporary buffer was about in your original
example - why not vfprintf? It would also have had problems with "%"
characters - the fprintf should have been fprintf(gDbgFP, "%s", dbgTmpStr).

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1728 727430
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
Nov 14 '05 #3
> C99 adds the feature of variadic macro arguments. Even if not fully
C99-conformant, a lot of compilers will probably support at least this
feature, as it's pretty easy:

#define DBG_WRITE(...) \
do { \
fprintf(gDbgFP, __VA_ARGS__); \
fflush(gDbgFP); \
} while (0)

The trailing macro parameter(s) that go in the position of the ellipsis
are
substituted into __VA_ARGS__. Unlike functions, it's legal to have the
ellipsis and no named parameters.

By the way, rather than have the fflush after every fprintf, it might be
neater to change the buffering of gDbgFP to be line-based, or make it
non-buffered, with a call to setvbuf.

I also don't understand what the temporary buffer was about in your
original
example - why not vfprintf? It would also have had problems with "%"
characters - the fprintf should have been fprintf(gDbgFP, "%s",
dbgTmpStr).

Thanks for the replies - doesn't look as if Visual Studio supports this :-(
Allan
Nov 14 '05 #4
"Allan Bruce" <ab****@TAKEMEAWAY.csd.abdn.ac.uk> wrote:
# Is there a way to make a pre-processor define with a variable number of
# arguements? I want to have something like this:
#ifdef DEBUGGING
void DBG_WRITE(char *A,...) {
va_list vl;
va_start(vl, A);
vfprintf(gDbgFP, A, vl);
va_end(vl);
fflush(gDbgFP);
}
#else
#define DBG_WRITE (void)
#endif

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I'm not even supposed to be here today.
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: fred | last post by:
I don't know if I'm doing this correctly. I have a little programming experience in python, c++ and some others but this is my first time with javascript. I'm trying have my website detect the...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
1
by: frank | last post by:
I am declaring a local buffer of a fixed size in my variable argument routine. How do I figure out the correct size of the variable argument list so that I can dynamically allocate the memory? ...
3
by: cody | last post by:
why foreach does always have to declare a new variable? I have to write foreach (int n in array){} but Iam not allowed to write: int n=0; foreach (n in array){}
1
by: stephane | last post by:
I have a problem which must be in this : print" <script type='text/javascript'> document.location.replace('http://127.0.0.1/add_task.php?req_id={$maxValue}&tk_request_name={$req_name}');...
9
by: ratika | last post by:
can anyone tell me how to delete a certain node in a doubly circular link list
1
by: skyson2ye | last post by:
Hi, guys: I have written a piece of code which utilizes Javascript in PHP to create a three level dynamic list box(Country, States/Province, Market). However, I have encountered a strange problem,...
6
by: tmallen | last post by:
What's the proper way to instantiate a new variable? x = ""?
9
by: Erwin Moller | last post by:
Hi all, Is it possible (PHP5.2) to find the name of a variable used in the caller of a function from within the function itself? Or to be more clear: ...php code.. $result = foo($abc);...
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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)...
0
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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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

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.