Printint out a macro's expansion

Discussion in 'C Programming' started by James H. Markowitz, Dec 25, 2009.

  1. 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, Dec 25, 2009
    #1
    1. Advertising

  2. 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.
    James H. Markowitz, Dec 25, 2009
    #2
    1. Advertising

  3. James H. Markowitz

    tusbar Guest

    On Dec 25, 7:58 pm, "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?


    Yes,

    -DMY_MACRO=\"abc\"

    --
    tusbar
    tusbar, Dec 25, 2009
    #3
  4. James H. Markowitz

    Nick Guest

    "James H. Markowitz" <> 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
    Nick, Dec 26, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Diego F.

    Printint datagrid: last row cut

    Diego F., Jun 8, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    377
    Diego F.
    Jun 8, 2005
  2. me
    Replies:
    1
    Views:
    1,122
    Victor Bazarov
    Nov 9, 2004
  3. Benjamin Niemann
    Replies:
    3
    Views:
    326
    Caleb Hattingh
    Aug 26, 2004
  4. Ark

    A question on macro expansion

    Ark, Jul 22, 2004, in forum: C Programming
    Replies:
    3
    Views:
    354
  5. printint problem

    , Oct 4, 2007, in forum: C++
    Replies:
    2
    Views:
    300
    Jim Langston
    Oct 5, 2007
Loading...

Share This Page