stuck with macro

Discussion in 'C Programming' started by Mark, Jan 12, 2011.

  1. Mark

    Mark Guest

    Hi

    I was reading http://c-faq.com/ansi/stringize.html I'd like to have a
    similar functionality for pasting tokens with '##' operator. The page says
    that the same approcah should be used for that. Could you give me a hint how
    to implement such macros:

    #define Str(x, y) x ## y /* two args paste together */
    #define XStr(x, y) Str(x, y)

    XStr expands first in to Str(), in the second step Str() expands into 'x'
    and 'y' merged together.
    How will I stringize the result of 'x ## y' ? I think there should be one
    more step in here, but where?

    Thanks in advance.

    --
    Mark
     
    Mark, Jan 12, 2011
    #1
    1. Advertising

  2. Mark

    Ian Collins Guest

    On 01/12/11 08:06 PM, Mark wrote:
    > Hi
    >
    > I was reading http://c-faq.com/ansi/stringize.html I'd like to have a
    > similar functionality for pasting tokens with '##' operator. The page
    > says that the same approcah should be used for that. Could you give me a
    > hint how to implement such macros:
    >
    > #define Str(x, y) x ## y /* two args paste together */
    > #define XStr(x, y) Str(x, y)
    >
    > XStr expands first in to Str(), in the second step Str() expands into
    > 'x' and 'y' merged together.
    > How will I stringize the result of 'x ## y' ? I think there should be
    > one more step in here, but where?


    How about

    #define Str(x, y) #x #y

    The two strings will then be concatenated by the preprocessor.

    --
    Ian Collins
     
    Ian Collins, Jan 12, 2011
    #2
    1. Advertising

  3. On Jan 12, 1:06 am, "Mark" <> wrote:
    > Hi
    >
    > I was readinghttp://c-faq.com/ansi/stringize.html  I'd like to have a
    > similar functionality for pasting tokens with '##' operator. The page says
    > that the same approcah should be used for that. Could you give me a hint how
    > to implement such macros:
    >
    > #define Str(x, y) x ## y        /* two args paste together */
    > #define XStr(x, y) Str(x, y)
    >
    > XStr expands first in to Str(), in the second step Str() expands into 'x'
    > and 'y' merged together.
    > How will I stringize the result of 'x ## y' ?  I think there should be one
    > more step in here, but where?
    >
    > Thanks in advance.
    >
    > --
    > Mark


    656(1)01:25 AM:~ 0> cat strpaste.h
    #define Str(x) #x
    #define SPaste(x, y) Str(x ## y)
    SPaste(ooga, booga)
    657(1)01:26 AM:~ 0> gcc -E strpaste.h
    # 1 "strpaste.h"
    # 1 "<built-in>"
    # 1 "<command-line>"
    # 1 "strpaste.h"


    "oogabooga"
    658(1)01:26 AM:~ 0>
     
    luser- -droog, Jan 12, 2011
    #3
  4. Mark

    Mark Guest

    pete wrote:
    > #include <stdio.h>
    >
    > #define str(s) # s
    >
    > #define XStr(x, y) str(x) ## str(y)
    >
    > int
    > main(void)
    > {
    > puts(XStr(A, x));
    > return 0;
    > }


    Compiler gives an error message:
    error: pasting ")" and "Str" does not give a valid preprocessing token

    --
    Mark
     
    Mark, Jan 12, 2011
    #4
  5. Mark

    Mark Guest

    Ian Collins wrote:
    >
    > How about
    >
    > #define Str(x, y) #x #y
    >
    > The two strings will then be concatenated by the preprocessor.


    This one looks good and very obvious, thank you!

    --
    Mark
     
    Mark, Jan 12, 2011
    #5
  6. Mark

    Mark Guest

    pete wrote:
    >> Compiler gives an error message:
    >> error: pasting ")" and "Str" does not give a valid preprocessing
    >> token

    >
    > There is no "Str" in the code that I posted.


    #include <stdio.h>

    #define str(s) # s

    #define XStr(x, y) str(x) ## str(y)

    int
    main(void)
    {
    puts(XStr(A, x));
    return 0;
    }

    #gcc -ansi -pedantic -W -Wall new.c && ./a.out
    new.c:10:1: error: pasting ")" and "str" does not give a valid preprocessing
    token

    --
    Mark
     
    Mark, Jan 12, 2011
    #6
  7. On Jan 12, 1:26 am, luser- -droog <> wrote:
    > On Jan 12, 1:06 am, "Mark" <> wrote:
    >
    >
    >
    > > Hi

    >
    > > I was readinghttp://c-faq.com/ansi/stringize.html I'd like to have a
    > > similar functionality for pasting tokens with '##' operator. The page says
    > > that the same approcah should be used for that. Could you give me a hint how
    > > to implement such macros:

    >
    > > #define Str(x, y) x ## y        /* two args paste together */
    > > #define XStr(x, y) Str(x, y)

    >
    > > XStr expands first in to Str(), in the second step Str() expands into 'x'
    > > and 'y' merged together.
    > > How will I stringize the result of 'x ## y' ?  I think there should be one
    > > more step in here, but where?

    >
    > > Thanks in advance.

    >
    > > --
    > > Mark

    >


    I made mine prettier!

    684(1)02:03 AM:~ 0> gcc -E - <<\#eof | sed '/^#/d;/^$/d' -
    > #define s(x) #x
    > #define p(x,y) s(x##y)
    > p(this,that)
    > #eof

    "thisthat"
    684(1)02:05 AM:~ 0>
     
    luser- -droog, Jan 12, 2011
    #7
  8. Mark

    Ian Collins Guest

    On 01/12/11 08:23 PM, pete wrote:
    > Mark wrote:
    >>
    >> Hi
    >>
    >> I was reading http://c-faq.com/ansi/stringize.html
    >> I'd like to have a
    >> similar functionality for pasting tokens with '##' operator.
    >> The page says
    >> that the same approcah should be used for that.
    >> Could you give me a hint how
    >> to implement such macros:
    >>
    >> #define Str(x, y) x ## y /* two args paste together */
    >> #define XStr(x, y) Str(x, y)
    >>
    >> XStr expands first in to Str(),
    >> in the second step Str() expands into 'x'
    >> and 'y' merged together.
    >> How will I stringize the result of 'x ## y' ?
    >> I think there should be one
    >> more step in here, but where?

    >
    > /* BEGIN new.c */
    >
    > #include<stdio.h>
    >
    > #define str(s) # s
    >
    > #define XStr(x, y) str(x) ## str(y)


    The ## is superfluous. So this distils down to my solution.

    --
    Ian Collins
     
    Ian Collins, Jan 12, 2011
    #8
    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. Dead RAM
    Replies:
    20
    Views:
    1,132
    John Harrison
    Jul 14, 2004
  2. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    590
    Jack Klein
    Sep 21, 2003
  3. sounak

    to get macro name from macro value

    sounak, Nov 22, 2005, in forum: C Programming
    Replies:
    17
    Views:
    512
    Mark McIntyre
    Nov 22, 2005
  4. Patrick Kowalzick
    Replies:
    5
    Views:
    485
    Patrick Kowalzick
    Mar 14, 2006
  5. Mike Manilone

    macro inside macro

    Mike Manilone, Oct 3, 2011, in forum: C Programming
    Replies:
    8
    Views:
    474
    Mike Manilone
    Oct 6, 2011
Loading...

Share This Page