Macro with a default value

A

aleksa

I would like to have a "dofor" macro which would use
either a suplied loop variable or a default loop variable (f).

dofor(512, i) // should be expanded to
for (i = 512; i != 0; i--)

dofor(512) // should be expanded to
for (f = 512; f != 0; f--)

Could someone give me some pointers here?
 
T

Tom St Denis

I would like to have a "dofor" macro which would use
either a suplied loop variable or a default loop variable (f).

dofor(512, i) // should be expanded to
for (i = 512; i != 0; i--)

dofor(512) // should be expanded to
for (f = 512; f != 0; f--)

Could someone give me some pointers here?

Good preprocessor question, but I'd like to just step in here and say:
"Don't write code like this."

Replacing single statements with macros is a surefire way to make your
code completely unmaintainable. Don't do it if you have any hope of
working on it long term.

Tom
 
S

Stefan Ram

aleksa said:
dofor(512, i) // should be expanded to
for (i = 512; i != 0; i--)
dofor(512) // should be expanded to
for (f = 512; f != 0; f--)

I does not work as follows,
but maybe someone can improve on this?

#define test(x) \
0/x/2; printf( "%s\n", #x ); if( 0 )\
+printf( "f\n" );

The idea is that with »test(i)«, this expands to

0/i/2; printf( "%s\n", "i" ); if( 0 )
+printf( "f\n" );

i.e., effectively,

printf( "%s\n", "i" );

, which should print the line »i«, while »test()«
expands to

0//2; printf( "%s\n", "i" ); if( 0 )
+printf( "f\n" );

, effectively,

0+printf( "f\n" );

, which should print the line »f«. However, it seems
that the »/« do not form a single »//« token this way.
 
E

Eric Sosman

I does not work as follows,
but maybe someone can improve on this?

#define test(x) \
0/x/2; printf( "%s\n", #x ); if( 0 )\
+printf( "f\n" );

The idea is that with »test(i)«, this expands to

0/i/2; printf( "%s\n", "i" ); if( 0 )
+printf( "f\n" );

i.e., effectively,

printf( "%s\n", "i" );

, which should print the line »i«, while »test()«
expands to

0//2; printf( "%s\n", "i" ); if( 0 )
+printf( "f\n" );

, effectively,

0+printf( "f\n" );

, which should print the line »f«. However, it seems
that the »/« do not form a single »//« token this way.

Right, because comments are recognized and removed in phase 3,
before the preprocessor does its work in phase 4. See section 5.1.1.2
"Translation phases."
 
S

Seebs

I would like to have a "dofor" macro which would use
either a suplied loop variable or a default loop variable (f).

dofor(512, i) // should be expanded to
for (i = 512; i != 0; i--)

dofor(512) // should be expanded to
for (f = 512; f != 0; f--)

Could someone give me some pointers here?

You can't do optional-argument macros.

Furthermore, don't do this. Several reasons, not the least of which is
that you're counting backwards which will usually surprise people, and
you should never hide simple basic syntax behind macros.

-s
 
K

Keith Thompson

Seebs said:
You can't do optional-argument macros.

Furthermore, don't do this. Several reasons, not the least of which is
that you're counting backwards which will usually surprise people, and
you should never hide simple basic syntax behind macros.

C99 added variadic macros. You *might* be able to do something like:

#define dofor(count, ...) /* something */

so at least both dofor(512, i) and dofor(512) would be legal. But I
can't think of a way to make this work for what the OP is asking about
(not that I tried very hard).

To the OP: If you're going to do this, at least use all-caps for the
macro name. (The convention of using all-caps for macro names is
intended to make them stand out, so it's obvious to the reader that
something strange is going on.)

For that matter, there's not that much benefit in using a single name
for both. If you want the loop variable name to default to i (yours
defaults to f, but i makes more sense), you could just write:

#define DOFOR(count, var) for ((var) = (count); (var) != 0; (var)--)
#define DOFORI(count) for (i = (count); i != 0; i--)

Or, if you can depend on having a C99 compiler, or at least one that
supports declarations in for loops as an extension:

#define DOFOR(count, var) for (int var = (count); (var) != 0; (var)--)
#define DOFORI(count) for (int i = (count); i != 0; i--)

You have to know how many arguments you're using when you write the
call; using distinct names isn't much of an added burden.

But these are all improvements on a bad idea. A better solution is
just to write ordinary for loops; they're going to be much more
legible to anyone reading your code (including you six months
from now).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top