macros with double pound signs (##)

Discussion in 'C Programming' started by davoti@gmail.com, Aug 28, 2007.

  1. Guest

    Hi,

    I never truly understand how a macro with ## work in C, for example,
    if I define

    #define X X##_YZ[2]

    what and how does this translated into after compilation?
    Can't find similar info. googling, would appreciate some detailed
    information.

    Thanks in advance.

    tom
     
    , Aug 28, 2007
    #1
    1. Advertising

  2. wrote:
    > Hi,
    >
    > I never truly understand how a macro with ## work in C, for example,
    > if I define
    >
    > #define X X##_YZ[2]


    Not very useful here, actually,

    The double pound sign is used for token concatenation:
    Example:

    #define BUILD_NAME(X,Y) X##.##Y

    BUILD_NAME("Big", "Jim")

    translates to:

    "Big.Jim"


    > Thanks in advance.



    --
    Pietro Cerutti

    PGP Public Key:
    http://gahr.ch/pgp
     
    Pietro Cerutti, Aug 28, 2007
    #2
    1. Advertising

  3. Guest

    On Aug 28, 10:13 am, Pietro Cerutti <> wrote:
    > wrote:
    > > Hi,

    >
    > > I never truly understand how a macro with ## work in C, for example,
    > > if I define

    >
    > > #define X X##_YZ[2]

    >
    > Not very useful here, actually,
    >
    > The double pound sign is used for token concatenation:
    > Example:
    >
    > #define BUILD_NAME(X,Y) X##.##Y
    >
    > BUILD_NAME("Big", "Jim")
    >
    > translates to:
    >
    > "Big.Jim"
    >
    > > Thanks in advance.

    >
    > --
    > Pietro Cerutti
    >
    > PGP Public Key:http://gahr.ch/pgp



    Thanks for the information, Sometimes it is used before the token,
    other time behind it, in your example, can I do like below? What's the
    difference?

    #define BUILD_NAME(X,Y) ##X.##Y

    Thanks again

    tom
     
    , Aug 28, 2007
    #3
  4. Ben Pfaff Guest

    Pietro Cerutti <> writes:

    > The double pound sign is used for token concatenation:
    > Example:
    >
    > #define BUILD_NAME(X,Y) X##.##Y
    >
    > BUILD_NAME("Big", "Jim")
    >
    > translates to:
    >
    > "Big.Jim"


    I'm pretty sure that it doesn't, actually.

    If you want to write code with that effect, you can much more
    simply write:
    #define BUILD_NAME(X,Y) X "." Y
    so that BUILD_NAME("Big", "Jim") translates to:
    "Big" "." "Jim"
    which the compiler will then concatenate into a single string,
    with the same effect as "Big.Jim".
    --
    "What is appropriate for the master is not appropriate for the novice.
    You must understand the Tao before transcending structure."
    --The Tao of Programming
     
    Ben Pfaff, Aug 28, 2007
    #4
  5. Ben Pfaff wrote:
    > Pietro Cerutti <> writes:
    >
    >> The double pound sign is used for token concatenation:
    >> Example:
    >>
    >> #define BUILD_NAME(X,Y) X##.##Y
    >>
    >> BUILD_NAME("Big", "Jim")
    >>
    >> translates to:
    >>
    >> "Big.Jim"

    >
    > I'm pretty sure that it doesn't, actually.
    >
    > If you want to write code with that effect, you can much more
    > simply write:
    > #define BUILD_NAME(X,Y) X "." Y
    > so that BUILD_NAME("Big", "Jim") translates to:
    > "Big" "." "Jim"
    > which the compiler will then concatenate into a single string,
    > with the same effect as "Big.Jim".


    You are right. I tried to think about the simplest and smallest example
    using token concatenation, and I failed miserably ;-)

    Next try:

    The token created by concatenating X and Y has to be itself a valid token:

    #define BUILD_MSG(X) msg_##X

    BUILD_MSG(hello)

    would translate to

    msg_hello

    --
    Pietro Cerutti

    PGP Public Key:
    http://gahr.ch/pgp
     
    Pietro Cerutti, Aug 28, 2007
    #5
  6. wrote:
    > On Aug 28, 10:13 am, Pietro Cerutti <> wrote:
    >> wrote:
    >>> Hi,
    >>> I never truly understand how a macro with ## work in C, for example,
    >>> if I define
    >>> #define X X##_YZ[2]

    >> Not very useful here, actually,
    >>
    >> The double pound sign is used for token concatenation:
    >> Example:
    >>
    >> #define BUILD_NAME(X,Y) X##.##Y
    >>
    >> BUILD_NAME("Big", "Jim")
    >>
    >> translates to:
    >>
    >> "Big.Jim"
    >>
    >>> Thanks in advance.

    >> --
    >> Pietro Cerutti
    >>
    >> PGP Public Key:http://gahr.ch/pgp

    >
    >
    > Thanks for the information, Sometimes it is used before the token,
    > other time behind it, in your example, can I do like below? What's the
    > difference?


    Please read my other post, in reply to Ben Pfaff, for corrections on the
    explanation :)

    The ## goes on the side you want your token to be pasted:


    > #define BUILD_NAME(X,Y) ##X.##Y


    You have two tokens, X and Y, which you want to concatenate, using a dot
    in between
    You want the dot to appear AFTER X, and Y appear after the dot
    -> X ## . ## Y

    >
    > Thanks again




    --
    Pietro Cerutti

    PGP Public Key:
    http://gahr.ch/pgp
     
    Pietro Cerutti, Aug 28, 2007
    #6
  7. On Aug 28, 10:02 pm, wrote:
    > Hi,
    >
    > I never truly understand how a macro with ## work in C, for example,
    > if I define
    >
    > #define X X##_YZ[2]
    >
    > what and how does this translated into after compilation?
    > Can't find similar info. googling, would appreciate some detailed
    > information.
    >


    In simple terms - It is for token concatenation / token pasting.
    Search using the terms "Token Concatenation in C" or "Token Pasting in
    C" or "## in C"

    Karthik Balaguru
     
    karthikbalaguru, Aug 28, 2007
    #7
  8. On Aug 28, 12:58 pm, Pietro Cerutti <> wrote:
    > > #define BUILD_NAME(X,Y) ##X.##Y

    >
    > You have two tokens, X and Y, which you want to concatenate, using a dot
    > in between
    > You want the dot to appear AFTER X, and Y appear after the dot
    > -> X ## . ## Y


    Although, to be precise, if you're using the dot to access a structure
    member (e.g. somestruct.value), no concatenation is needed, as it is
    made up of three separate tokens. I'm not sure if token concatenation
    would work for constructing floating-point numbers, but I imagine it
    potentially could.
     
    Justin Spahr-Summers, Aug 28, 2007
    #8
  9. user923005 Guest

    On Aug 28, 10:02 am, wrote:
    > Hi,
    >
    > I never truly understand how a macro with ## work in C, for example,
    > if I define
    >
    > #define X X##_YZ[2]
    >
    > what and how does this translated into after compilation?
    > Can't find similar info. googling, would appreciate some detailed
    > information.


    Do a web search for "Token pasting operator"
     
    user923005, Aug 29, 2007
    #9
    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. Mark Rae
    Replies:
    0
    Views:
    863
    Mark Rae
    Mar 14, 2006
  2. Sydex
    Replies:
    12
    Views:
    6,505
    Victor Bazarov
    Feb 17, 2005
  3. Replies:
    80
    Views:
    2,432
    Stephen J. Bevan
    Nov 7, 2003
  4. Replies:
    1
    Views:
    448
    Marco Antoniotti
    Oct 7, 2003
  5. Replies:
    6
    Views:
    180
    Bob Barrows [MVP]
    May 5, 2007
Loading...

Share This Page