Token pasting (## operator) - Add whitespace to a token

Discussion in 'C Programming' started by Wessi, Aug 11, 2005.

  1. Wessi

    Wessi Guest

    Hi,

    token pasting means, that normally whitespaces and comments are deleted
    before and after the ## operator. I want the preprocessor to hold an
    existing whitespace while replacing the argument of a macro. Example:

    The following define and macro

    #define ISR_CAT1(NAME) __interrupt void ##NAME(void)

    ISR_CAT1(myIsrHandler)
    {

    }

    should be expanded to

    __interrupt void myIsrHandler(void)
    {

    }

    But the preprocessor does the following replacement (deleting
    whitespaces before ##):

    __interrupt voidmyIsrHandler(void)
    {

    }

    Is there any possibility to avoid this kind of replacement/deleting the
    whitespaces in front of ## operator?
    Wessi, Aug 11, 2005
    #1
    1. Advertising

  2. Wessi

    Guest

    Wessi wrote:
    > Hi,
    >
    > token pasting means, that normally whitespaces and comments are deleted
    > before and after the ## operator. I want the preprocessor to hold an
    > existing whitespace while replacing the argument of a macro. Example:
    >
    > The following define and macro
    >
    > #define ISR_CAT1(NAME) __interrupt void ##NAME(void)
    >
    > ISR_CAT1(myIsrHandler)
    > {
    >
    > }
    >
    > should be expanded to
    >
    > __interrupt void myIsrHandler(void)
    > {
    >
    > }
    >
    > But the preprocessor does the following replacement (deleting
    > whitespaces before ##):
    >
    > __interrupt voidmyIsrHandler(void)
    > {
    >
    > }
    >
    > Is there any possibility to avoid this kind of replacement/deleting the
    > whitespaces in front of ## operator?


    Why are you using "##" ? Following definition should work.
    #define ISR_CAT1(NAME) __interrupt void NAME(void)
    , Aug 11, 2005
    #2
    1. Advertising

  3. Wessi

    Wessi Guest

    Thanks, you're right. I had another problem with double macro
    expansion, which made me believe, that your solution will not work.
    Wessi, Aug 11, 2005
    #3
  4. On Thu, 11 Aug 2005 04:40:12 -0700, Wessi wrote:

    > Hi,
    >
    > token pasting means, that normally whitespaces and comments are deleted
    > before and after the ## operator. I want the preprocessor to hold an
    > existing whitespace while replacing the argument of a macro. Example:


    The result of token pasing must be a valid pp-token and only pp-tokens
    like character constants and string literals can contain spaces. Trying to
    create another type of pp-token containing spaces doesn't make any sense.

    > The following define and macro
    >
    > #define ISR_CAT1(NAME) __interrupt void ##NAME(void)
    >
    > ISR_CAT1(myIsrHandler)
    > {
    >
    > }
    >
    > should be expanded to
    >
    > __interrupt void myIsrHandler(void)
    > {
    >
    > }


    Token pasting is used to create new (composite) tokens but there aren't
    any such tokens in your desired expansion; your error is in trying to use
    token pasting when it is not appropriate. Use simply:

    #define ISR_CAT1(NAME) __interrupt void NAME(void)

    > But the preprocessor does the following replacement (deleting
    > whitespaces before ##):
    >
    > __interrupt voidmyIsrHandler(void)
    > {
    >
    > }


    Yes, your macro tries to paste the tokens void and myIsrHandler into a
    single one whereas what you really need is just to leave them as separate
    tokens.

    > Is there any possibility to avoid this kind of replacement/deleting the
    > whitespaces in front of ## operator?


    Don't use token pasting.

    Lawrence
    Lawrence Kirby, Aug 11, 2005
    #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. Mark Odell

    Token-pasting trouble

    Mark Odell, Apr 6, 2006, in forum: C Programming
    Replies:
    8
    Views:
    365
    Mark Odell
    Apr 6, 2006
  2. Mark Odell
    Replies:
    7
    Views:
    358
    Mark Odell
    May 1, 2006
  3. Henry Townsend

    token pasting problem in K&R preprocessor

    Henry Townsend, Jun 19, 2006, in forum: C Programming
    Replies:
    13
    Views:
    649
  4. Jean-Claude Gervais
    Replies:
    3
    Views:
    464
    Michael Olea
    Jun 27, 2005
  5. token pasting help (##)

    , Jun 22, 2007, in forum: C Programming
    Replies:
    2
    Views:
    513
    Keith Thompson
    Jun 22, 2007
Loading...

Share This Page