cpp: getting to the definition of a defined constant

S

Sam Steingold

The following code:

#define DEF3 DEF4
#define DEF2 DEF3
#define DEF1 DEF2
#define STRING(x) #x
#define SHOW(x) puts("#define " #x " " STRING(x))

void main () {
SHOW(DEF1);
SHOW(DEF2);
SHOW(DEF3);
SHOW(DEF4);
}

prints

#define DEF1 DEF4
#define DEF2 DEF4
#define DEF3 DEF4
#define DEF4 DEF4

what I want is to see that DEF1 is originally defined to DEF2.

Is there a way to get

#define DEF1 DEF2

printed, instead of

#define DEF1 DEF4

(i.e., I am getting the behavior of Lisp macroexpand,
while what I want is macroexpand-1 instead,
see http://www.lisp.org/HyperSpec/Body/fun_macroexpa_acroexpand-1.html)

Thanks!

PS. I also try to post to comp.lang.c.moderated,
but it does not appear there...
 
L

lawrence.jones

Sam Steingold said:
#define DEF1 DEF2

printed, instead of

#define DEF1 DEF4

No. C only allows no expansion at all or full expansion, it does not
provide any facility for just one level of expansion.

-Larry Jones

I hope Mom and Dad didn't rent out my room. -- Calvin
 
W

Walter Roberson

No. C only allows no expansion at all or full expansion, it does not
provide any facility for just one level of expansion.[/QUOTE]

The # operator provides one level of expansion.

#define FOO(x) #x

This does NOT result in "x" such as would be the case if
"no expansion at all" were taking place.

The hard part is in expanding exactly -two- levels -- to get the
"first level replacement" associated with a parameter whose name
has been passed via a dummy argument.
 
S

Sam Steingold

* Walter Roberson said:
No. C only allows no expansion at all or full expansion, it does not
provide any facility for just one level of expansion.

The # operator provides one level of expansion.

#define FOO(x) #x

This does NOT result in "x" such as would be the case if
"no expansion at all" were taking place.

The hard part is in expanding exactly -two- levels -- to get the
"first level replacement" associated with a parameter whose name
has been passed via a dummy argument.[/QUOTE]

OK, I could do with explicitly writing

#define Z(x) ((x)+(x))

puts("#define Z(y) " #Z(y));

to get "#define Z(y) ((y)+(y))" printed, but that does not work either.
 
L

lawrence.jones

Walter Roberson said:
The # operator provides one level of expansion.

#define FOO(x) #x

This does NOT result in "x" such as would be the case if
"no expansion at all" were taking place.

That's argument substitution, not macro expansion.

-Larry Jones

What this games needs are negotiated settlements. -- Calvin
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top