Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Printint out a macro's expansion (http://www.velocityreviews.com/forums/t709852-printint-out-a-macros-expansion.html)

James H. Markowitz 12-25-2009 06:58 PM

Printint out a macro's expansion
 
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?


James H. Markowitz 12-25-2009 07:18 PM

Re: Printint out a macro's expansion
 
On Fri, 25 Dec 2009 18:58:46 +0000, James H. Markowitz wrote:

> 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.


tusbar 12-25-2009 07:21 PM

Re: Printint out a macro's expansion
 
On Dec 25, 7:58*pm, "James H. Markowitz" <no...@nowhere.net> wrote:
> * * * * 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\"

--
tusbar

Nick 12-26-2009 11:12 AM

Re: Printint out a macro's expansion
 
"James H. Markowitz" <noone@nowhere.net> writes:

> 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.
--
Online waterways route planner: http://canalplan.org.uk
development version: http://canalplan.eu


All times are GMT. The time now is 06:31 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.