about macros

G

gjhdgm

$ cat t.c

#define B A
#define C B
#define P(x) p_##x
#define Q(x) P(x)
P(C)
Q(C)

$ gcc -E t.c
p_C
p_A

so, P(C) becomes p_C, Q(C) becomes p_A.
how to write a macro R(x) to make R(C) becoming p_B?
assume that A B and C are already defined, and the patament of the macro R can only be C.

thanks.
 
H

Harald van Dijk

$ cat t.c

#define B A
#define C B
#define P(x) p_##x
#define Q(x) P(x)
P(C)
Q(C)

$ gcc -E t.c
p_C
p_A

For the record, this invokes gcc in a mode which does not conform to
any standard. And your output is different from mine in default and
conforming modes, in that my output includes location information and
blank lines.
so, P(C) becomes p_C, Q(C) becomes p_A.
how to write a macro R(x) to make R(C) becoming p_B?

There is no general answer for this. Obviously
#define R(x) p_B
would expand to p_B, but if the B comes from a macro argument, either
it gets expanded, or it doesn't. Only recursive macro definitions --
which wouldn't help you -- can cause a partial macro expansion.
assume that A B and C are already defined, and the patament of the macro R can only be C.

*If* you really, really, really need this, you may be best of using a
non-C text macro processor such as m4, but perhaps if you explain why
you want this, there's a better solution.
 
J

J. J. Farrell

gjhdgm said:
$ cat t.c

#define B A
#define C B
#define P(x) p_##x
#define Q(x) P(x)
P(C)
Q(C)

$ gcc -E t.c
p_C
p_A

so, P(C) becomes p_C, Q(C) becomes p_A.
how to write a macro R(x) to make R(C) becoming p_B?
assume that A B and C are already defined, and the patament of the macro R can only be C.

thanks.

What's a "patament", and who set the question?
 
G

gjhdgm

For the record, this invokes gcc in a mode which does not conform to
any standard. And your output is different from mine in default and
conforming modes, in that my output includes location information and
blank lines.


There is no general answer for this. Obviously
  #define R(x) p_B
would expand to p_B, but if the B comes from a macro argument, either
it gets expanded, or it doesn't. Only recursive macro definitions --
which wouldn't help you -- can cause a partial macro expansion.


*If* you really, really, really need this, you may be best of using a
non-C text macro processor such as m4, but perhaps if you explain why
you want this, there's a better solution.

I'm just curious about this, and I think m4 would solve this problem.
You can ignore the following. Thank you for your answer.

==========
I'm using a lib, it defined macros like AA CLOCK_AA PORT_AA BB
CLOCK_BB and PORT_BB.

I want to write a file config.h, with a line "#define XX ...", and
with some other macros like CLOCK_XX and PORT_XX.
when I defines XX to AA, CLOCK_XX and PORT_XX becomes CLOCK_AA and
PORT_AA
when I defines XX to BB, CLOCK_XX and PORT_XX becomes CLOCK_BB and
PORT_BB
So if I'm using AA now and I want to change it to BB, I only need to
change the definition of XX.

Maybe I'm thinking in the way with bash's command eval.
 

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,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top