Printint out a macro's expansion

J

James H. Markowitz

This may be an easy one for those who know, but I just can't
figure it out.

I have a source file S.c that will be compiled twice into the
same executable. In one occasion it is compiled with the compile time
macro definition -DMY_MACRO=abc, whereas in the other the macro
definition is -DMY_MACRO=xyz.

Inside S.c I'd like to have a line of code such that as a result
of the first compilation it would print out

"MY_MACRO is abc."

whereas for the other it would print out

"MY_MACRO is xyz."

Something like

printf("MY_MACRO is %s.", MY_MACRO) ;

does not work, because abc and xyz are not strings. I had a go with the
stringification feature, but so far without success.

Suggestions?
 
J

James H. Markowitz

This may be an easy one for those who know, but I just can't figure it
out.

I have a source file S.c that will be compiled twice into the
same executable. In one occasion it is compiled with the compile time
macro definition -DMY_MACRO=abc, whereas in the other the macro
definition is -DMY_MACRO=xyz.

Inside S.c I'd like to have a line of code such that as a result
of the first compilation it would print out

"MY_MACRO is abc."

whereas for the other it would print out

"MY_MACRO is xyz."

Something like

printf("MY_MACRO is %s.", MY_MACRO) ;

does not work, because abc and xyz are not strings. I had a go with the
stringification feature, but so far without success.

Suggestions?

Never mind; I've found it. One defines two macros as follows:

#define FIRST_MACRO(X) SECOND_MACRO(X)
#define SECOND_MACRO(X) #X

With this, a line like

printf("%s\n", FIRST_MACRO(MY_MACRO)) ;

will expand to

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

and

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

respectively, in the examples I mentioned.
 
T

tusbar

        This may be an easy one for those who know, but I just can't
figure it out.

        I have a source file S.c that will be compiled twice into the
same executable. In one occasion it is compiled with the compile time
macro definition -DMY_MACRO=abc, whereas in the other the macro
definition is -DMY_MACRO=xyz.

        Inside S.c I'd like to have a line of code such that as a result
of the first compilation it would print out

        "MY_MACRO is abc."

whereas for the other it would print out

        "MY_MACRO is xyz."

        Something like

        printf("MY_MACRO is %s.", MY_MACRO) ;

does not work, because abc and xyz are not strings. I had a go with the
stringification feature, but so far without success.

        Suggestions?

Yes,

-DMY_MACRO=\"abc\"
 
N

Nick

James H. Markowitz said:
On Fri, 25 Dec 2009 18:58:46 +0000, James H. Markowitz wrote:

Never mind; I've found it. One defines two macros as follows:

#define FIRST_MACRO(X) SECOND_MACRO(X)
#define SECOND_MACRO(X) #X

With this, a line like

printf("%s\n", FIRST_MACRO(MY_MACRO)) ;

will expand to

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

and

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

respectively, in the examples I mentioned.

As it is expanding to a fixed string, you can simplify that to

puts(FIRST_MACRO(MY_MACRO));

That applies even if you want to say more than just the name:

puts("I am now parsing" FIRST_MACRO(MY_MACRO));

will work because two adjacent string literals will be concatenated.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top