473,406 Members | 2,371 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,406 software developers and data experts.

Question about #define

Hello

I would like to write a macro like that:

#ifdef DEBUG
#define printj(...) printf(...)
#else
#printj(...)
#endif

So that printj behaves exactly like printf when DEBUG is true and
doesnt do anything when DEBUG is false.
But I dont know how to deal with the variable number of arguments of
printf(...).

Any help on how should be my "define"??

Thanks,

Jorge
Nov 14 '05 #1
15 2145
You can use:

#ifdef DEBUG
#define printj(x) printf((x))
#else
#define printj(x)
#endif

but now you should use printj as: printj(("just a test\n"));

AFAIK macros don't support a variable number of arguments.

Frank

"Jorge Naxus" <eg*******@terra.es> wrote in message
news:dc**************************@posting.google.c om...
Hello

I would like to write a macro like that:

#ifdef DEBUG
#define printj(...) printf(...)
#else
#printj(...)
#endif

So that printj behaves exactly like printf when DEBUG is true and
doesnt do anything when DEBUG is false.
But I dont know how to deal with the variable number of arguments of
printf(...).

Any help on how should be my "define"??

Thanks,

Jorge

Nov 14 '05 #2
On 1 Mar 2004 07:10:01 -0800, eg*******@terra.es (Jorge Naxus) wrote:
Hello

I would like to write a macro like that:

#ifdef DEBUG
#define printj(...) printf(...)
#else
#printj(...)
#endif

So that printj behaves exactly like printf when DEBUG is true and
doesnt do anything when DEBUG is false.
But I dont know how to deal with the variable number of arguments of
printf(...).

Any help on how should be my "define"??
C99 does (as I just discovered to my complete surprise by Googling)
support variadic macros! So if you're coding in C99 you're all set.

Otherwise, I'd probably approach this by creating an ordinary (non-macro)
variadic printj function inside of which, via conditional compilation
(based presumably on your DEBUG symbol), either hands off to a
printf-family function (e.g. vprintf) or doesn't.
-leor

Thanks,

Jorge


Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #3
Jorge Naxus wrote:

I would like to write a macro like that:

#ifdef DEBUG
#define printj(...) printf(...)
#else
#printj(...)
#endif

So that printj behaves exactly like printf when DEBUG is true and
doesnt do anything when DEBUG is false. But I dont know how to
deal with the variable number of arguments of printf(...).

Any help on how should be my "define"??


You have two choices.

1. Get and use a C99 compiler. Then your code will no longer port
to a C90 compiler.

2. Get and use gcc and the gnu variadic macro extensions. Then
your code will no longer port to a non-gnu compiler. If you get a
late enough gcc you can combine with option 1, because the latest
gccs handle C99 variadic macros in addition to the gnu flavor.

The only example I can show you is in nmalloc.zip, available on my
page, download section. It is using exactly those facilities, and
is thus limited to gcc compilation.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #4
news:dc**************************@posting.google.c om...
Hello

I would like to write a macro like that:

#ifdef DEBUG
#define printj(...) printf(...)
#else
#printj(...)
#endif

So that printj behaves exactly like printf when DEBUG is true and
doesnt do anything when DEBUG is false.
But I dont know how to deal with the variable number of arguments of
printf(...).

Any help on how should be my "define"??

Thanks,

Jorge


I normally end up creating about 5 seporate macros for the task.

#ifdef DEBUGMODE
#define DEBUG_PRINT(a) printf(a)
#define DEBUG_PRINT1(a,b) printf(a,b)

etc, etc..

inelegant, but it works fine for me.
Nov 14 '05 #5
In article <40***********************@news.versatel.net>,
"Frank van Eijkelenburg" <so*****@work.com> wrote:
#ifdef DEBUG
#define printj(x) printf((x))
#else
#define printj(x)
#endif

but now you should use printj as: printj(("just a test\n"));


probably Frank meant:

#ifdef DEBUG
#define printj(x) printf x
#else
#define printj(x)
#endif

but now you should use printj as:
printj(("just a test\n"));
printj(("%d %d %d\n",1,2,3));

and must NOT use
printj(("%d\n",++j));

[not tested]

François Grieu
Nov 14 '05 #6
In article <dc**************************@posting.google.com >,
Jorge Naxus <eg*******@terra.es> wrote:
Hello

I would like to write a macro like that:

#ifdef DEBUG
#define printj(...) printf(...)
#else
#printj(...)
#endif

So that printj behaves exactly like printf when DEBUG is true and
doesnt do anything when DEBUG is false.
But I dont know how to deal with the variable number of arguments of
printf(...).

Any help on how should be my "define"??

Thanks,

Jorge


I see two possible solutions.
1. If you are using a C99 compatable compiler, then what you want is

#ifdef DEBUG
#define printj(...) printf(__VA_ARGS__)
#else
#define printj(...) ((void)0)
#endif

2. If you are using a C89 compatable compiler, then the following will work.
#ifdef DEBUG
#define printj printf
#else
#define printj (void)
#endif
The 2nd method requires a little bit of explaining and has some drawbacks.
When DEBUG is defined, I believe that you'll see that it does what you want.
If DEBUG isn't defined, what you'll see is that you get a series of comma
seperated expressions inside parens and the final result of these expressions
is then cast to void. This behaivor is also what you want. The drawback
is that the printj macro isn't a function-like macro and therefore everywhere
the compiler sees printj, it will make the subsitution. For example, look
at the following code:

int main(void)
{
int printj;

for(printj=0; printj < 10; ++printj) {
printf("%d\n", printj);
}
return 0;
}

The above example is perfectly legal using the macros for C99, but will fail
to compile using the macros for C89.

Hope this helps,
John Cochran
Nov 14 '05 #7
John Cochran <jd*@smof.fiawol.org> wrote:
#ifdef DEBUG
#define printj printf
#else
#define printj (void)
#endif
The 2nd method requires a little bit of explaining and has some drawbacks.
When DEBUG is defined, I believe that you'll see that it does what you want.
If DEBUG isn't defined, what you'll see is that you get a series of comma
seperated expressions inside parens and the final result of these expressions
is then cast to void. This behaivor is also what you want. The drawback


One other drawback is that all sub-expressions within the comma-expression
are evaluated, all side-effects apply, and that might not be what
you want.

--
Stan Tobias
Nov 14 '05 #8
In article <c2*************@ID-223330.news.uni-berlin.de>,
Stan Tobias <sN*******@amu.edu.pl> wrote:
John Cochran <jd*@smof.fiawol.org> wrote:
#ifdef DEBUG
#define printj printf
#else
#define printj (void)
#endif


The 2nd method requires a little bit of explaining and has some drawbacks.
When DEBUG is defined, I believe that you'll see that it does what you want.
If DEBUG isn't defined, what you'll see is that you get a series of comma
seperated expressions inside parens and the final result of these expressions
is then cast to void. This behaivor is also what you want. The drawback


One other drawback is that all sub-expressions within the comma-expression
are evaluated, all side-effects apply, and that might not be what
you want.


True, but if you have side effects in your debug statements, then something
is seriously wrong with your code and/or design because you will get different
results from your code depending on if debug is enabled or not.

Nov 14 '05 #9
> I would like to write a macro like that:

#ifdef DEBUG
#define printj(...) printf(...)
#define printj printf
#else
#printj(...)
#define printj
#endif

So that printj behaves exactly like printf when DEBUG is true and
doesnt do anything when DEBUG is false.


NB. Avoid using printj's return value

NB2. You say "doesnt do anything". All the solutions offered so far
do evaluate printj's arguments (eg. if you went: printj("%d", foo());
then foo gets called). To prevent this you will have to use the
normal method of #ifdef etc. around each printf.
Nov 14 '05 #10

On Mon, 1 Mar 2004, Old Wolf wrote:
I would like to write a macro like that:

#ifdef DEBUG
#define printj(...) printf(...)
#define printj printf
#else
#printj(...)


#define printj
#endif

So that printj behaves exactly like printf when DEBUG is true and
doesnt do anything when DEBUG is false.


NB. Avoid using printj's return value


....because in reality 'printj' doesn't have a return value; it has
a textual expansion whose type at compile-time depends on the value
of DEBUG. If (DEBUG != 0), then sure, you can use the return value
of 'printj' (a.k.a. 'printf'). If (DEBUG == 0), then you have to
jump through many more hoops to get any useful information out of
that comma expression you've [presumably] got there.
NB2. You say "doesnt do anything". All the solutions offered so far
do evaluate printj's arguments (eg. if you went: printj("%d", foo());
then foo gets called). To prevent this you will have to use the
normal method of #ifdef etc. around each printf.


No, there are a couple of hacks for this situation. One silly
and not-quite-foolproof one is

#ifndef DEBUG
#define printj (int)sizeof
#endif

This fails in the (hopefully unlikely) case of

printj("foo")[i++];

which should increment 'i', but with the current definition actually
does not. (Note that I've chosen to make 'printj' evaluate to type
'int', so that its type doesn't change when 'DEBUG' changes. That's
a style issue, I'm sure, and in real code I probably wouldn't even
do it.)

I was trying to compose a solution that would involve stringizing
the 'printj' "argument," but that really doesn't seem possible
(as long as we must keep the semantics that 'printj' be a drop-in
replacement for 'printf' in ordinary code).

-Arthur

Nov 14 '05 #11
On 1 Mar 2004 07:10:01 -0800, eg*******@terra.es (Jorge Naxus) wrote:

Thanks to all the people who has helped me on these question, I have
learned a lot.

I dont have a C99 compiler (I am cross compiling), so I have used the
method proposed by John Cochran

#ifdef DEBUG
#define printj printf
#else
#define printj (void)
#endif

Although other possibilities you have pointed out would also be valid.

Acttually I dont change anything in my printf statements because these
are only for debugging purposes, so I dont mind if there are evaluated
or not.

I hate to read the C sources plenty of #ifdef around if statement, so
I think my code will be more beautiful now
Thanks!,

Jorge
Nov 14 '05 #12

In article <84**************************@posting.google.com >, ol*****@inspire.net.nz (Old Wolf) writes:
I would like to write a macro like that:

#ifdef DEBUG


#define printj printf
#else


#define printj
#endif

So that printj behaves exactly like printf when DEBUG is true and
doesnt do anything when DEBUG is false.


NB. Avoid using printj's return value

NB2. You say "doesnt do anything". All the solutions offered so far
do evaluate printj's arguments (eg. if you went: printj("%d", foo());
then foo gets called). To prevent this you will have to use the
normal method of #ifdef etc. around each printf.


I'd never use something like this, but how about one of:

#define printj 0 &&
#define printj 1 ||

for the non-DEBUG case, as a solution that doesn't evaluate the
arguments for subexpressions (not necessarily what's wanted, of
course) and evaluates to a known value (so the "return value"
is usable).

Or am I missing some obvious case where this construct fails
(as well as being ugly and obscuring the code, of course)?

--
Michael Wojcik mi************@microfocus.com

Even though there may be some misguided critics of what we're trying
to do, I think we're on the wrong path. -- Reagan
Nov 14 '05 #13
In article <84**************************@posting.google.com >,
Old Wolf <ol*****@inspire.net.nz> wrote:
I would like to write a macro like that:

#ifdef DEBUG
#define printj(...) printf(...)


#define printj printf
#else
#printj(...)


#define printj
#endif

So that printj behaves exactly like printf when DEBUG is true and
doesnt do anything when DEBUG is false.


NB. Avoid using printj's return value

NB2. You say "doesnt do anything". All the solutions offered so far
do evaluate printj's arguments (eg. if you went: printj("%d", foo());
then foo gets called). To prevent this you will have to use the
normal method of #ifdef etc. around each printf.


I've used this for more years than I care to admit:

#ifdef DEBUG
#define PRINT(arglist) fprintf arglist
#else
#define PRINT(arglist)
#endif

and then use it like:

PRINT((stderr, "foobar: %d\n", __LINE__));

Nov 14 '05 #14
Jorge <te***@egrojorge.es> wrote:
# On 1 Mar 2004 07:10:01 -0800, eg*******@terra.es (Jorge Naxus) wrote:
#
# Thanks to all the people who has helped me on these question, I have
# learned a lot.
#
# I dont have a C99 compiler (I am cross compiling), so I have used the
# method proposed by John Cochran
#
# #ifdef DEBUG
# #define printj printf
# #else
# #define printj (void)
# #endif
#
# Although other possibilities you have pointed out would also be valid.

If printj is only used as statement you can use
#define printj if(0)

If the argument list side effect free, many compilers and any optimising
compiler will recognise the code is dead and elide it.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
Raining down sulphur is like an endurance trial, man. Genocide is the
most exhausting activity one can engage in. Next to soccer.
Nov 14 '05 #15

On Wed, 3 Mar 2004, Derk Gwen wrote:

Jorge <te***@egrojorge.es> wrote:
# On 1 Mar 2004 07:10:01 -0800, eg*******@terra.es (Jorge Naxus) wrote:
#
# Thanks to all the people who has helped me on these question, I have
# learned a lot.
#
# I dont have a C99 compiler (I am cross compiling), so I have used the
# method proposed by John Cochran
#
# #ifdef DEBUG
# #define printj printf
# #else
# #define printj (void)
# #endif
#
# Although other possibilities you have pointed out would also be valid.

If printj is only used as statement you can use
#define printj if(0)

if (somecondition)
printj("somecondition checks out\n");
else
printj("somecondition is false; or else it isn't, and !DEBUG\n");

Whoops! Better you should have said "If printj is only used inside
{ }, or in harmless situations...", and that's a lot harder to get
right.

-Arthur
Nov 14 '05 #16

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

Similar topics

4
by: ethan | last post by:
Hi All, I'd like ask some question about macro. If I define a variable set, such as {5, 2, 10} or {1,3}. #define X {5,2,10} 1) Is it possible to write a macro to get the number of items...
2
by: Jack David | last post by:
Using the code below I am able to monitor a single directory for a new file and then kick-off a process to deal with the file. The question is??? How would I modify this code to be able to monitor...
2
by: Gustavo L. Fabro | last post by:
Greetings. Is there a way to run the preprocessor twice? Rephrasing that, is there a way to: #define SOMETHING #pragma OTHERTHING and have the preprocessor in somecode.cpp evaluate: ...
0
by: Mike hofer | last post by:
I am studying up for my MCAD, and came across an interesting conundrum. According to my textbook, there are FOUR steps to publish an event: 1. Define a delegate type that specifies the prototype...
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
5
by: ma740988 | last post by:
Consider: #include "handyfactory.h" #include <iostream> struct Shape { virtual void print() const=0; };
6
by: AzizMandar | last post by:
There is probably a better way to do this and if so I'm just as happy to see that way. I have a program where I have factories that each create various objects abstracted from a base class. ...
3
by: kkk | last post by:
I am practicing LRU by writing a simulating c programme, but encounter a problem. The problem is when a page hit occurred (simulated process request (contains requested page) a page, which has...
8
by: Fred | last post by:
I've got following program encapsuled fscanf, however, it doesn't work. I'm sure that the content format in "a.txt" is OK, the content would be correctly read if using fscanf directly, what's...
6
by: damiensawyer | last post by:
Hi, Can someone please explain to me something about delegates? My understanding is as follows. A delegate is basically an object that can hold a reference to a "method" somewhere. That is,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.