Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Printint out a macro's expansion

Reply
Thread Tools

Printint out a macro's expansion

 
 
James H. Markowitz
Guest
Posts: n/a
 
      12-25-2009
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?

 
Reply With Quote
 
 
 
 
James H. Markowitz
Guest
Posts: n/a
 
      12-25-2009
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.

 
Reply With Quote
 
 
 
 
tusbar
Guest
Posts: n/a
 
      12-25-2009
On Dec 25, 7:58*pm, "James H. Markowitz" <(E-Mail Removed)> 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
 
Reply With Quote
 
Nick
Guest
Posts: n/a
 
      12-26-2009
"James H. Markowitz" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Expansion of the Sun X-Terminal Mod bigal Case Modding 85 11-07-2008 11:02 AM
printint problem grohotanie@gmail.com C++ 2 10-04-2007 11:35 PM
Printint datagrid: last row cut Diego F. ASP .Net 0 06-08-2005 04:17 PM
Wireless network and Age of Empires Expansion multiplayer =?Utf-8?B?Qm9iIFcu?= Wireless Networking 1 04-05-2005 07:31 PM
Photoshop Printint Problems Robert Desel Digital Photography 3 01-20-2004 07:01 AM



Advertisments