Preprocessor define concatenation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • michael.martin@asahq.com

    #1

    Preprocessor define concatenation

    Hi,

    I am wondering if something like this is possible?

    #define COLON ":"
    #define PERIOD "."

    #define WORD "word"

    #define WORD_COLON WORD+COLON
    #define WORD_PERIOD WORD+PERIOD

    ....

    I know the above is incorrect syntax. But is there a way to
    concatenate the above strings in some way? I would rather not have to
    define WORD_COLON as "word:" and WORD_PERIOD as "word.". I would like
    to be able to change the value of WORD, with just modifying one
    constant, rather than modifying all 3, as I am doing this on a much
    larger scale.

    Thanks for any help,
    mike martin

  • Joona I Palaste

    #2
    Re: Preprocessor define concatenation

    michael.martin@ asahq.com scribbled the following:[color=blue]
    > Hi,[/color]
    [color=blue]
    > I am wondering if something like this is possible?[/color]
    [color=blue]
    > #define COLON ":"
    > #define PERIOD "."[/color]
    [color=blue]
    > #define WORD "word"[/color]
    [color=blue]
    > #define WORD_COLON WORD+COLON
    > #define WORD_PERIOD WORD+PERIOD[/color]
    [color=blue]
    > ...[/color]
    [color=blue]
    > I know the above is incorrect syntax. But is there a way to
    > concatenate the above strings in some way? I would rather not have to
    > define WORD_COLON as "word:" and WORD_PERIOD as "word.". I would like
    > to be able to change the value of WORD, with just modifying one
    > constant, rather than modifying all 3, as I am doing this on a much
    > larger scale.[/color]

    The C compiler concatenates any adjacent string literals into a single
    string literal. Therefore
    #define WORD_COLON WORD COLON
    #define WORD_PERIOD WORD PERIOD
    should do the trick.

    --
    /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "We're women. We've got double standards to live up to."
    - Ally McBeal

    Comment

    • Yan

      #3
      Re: Preprocessor define concatenation

      Joona I Palaste wrote:[color=blue]
      > michael.martin@ asahq.com scribbled the following:
      >[color=green]
      >>Hi,[/color]
      >
      >[color=green]
      >>I am wondering if something like this is possible?[/color]
      >
      >[color=green]
      >>#define COLON ":"
      >>#define PERIOD "."[/color]
      >
      >[color=green]
      >>#define WORD "word"[/color]
      >
      >[color=green]
      >>#define WORD_COLON WORD+COLON
      >>#define WORD_PERIOD WORD+PERIOD[/color]
      >
      >[color=green]
      >>...[/color]
      >
      >[color=green]
      >>I know the above is incorrect syntax. But is there a way to
      >>concatenate the above strings in some way? I would rather not have to
      >>define WORD_COLON as "word:" and WORD_PERIOD as "word.". I would like
      >>to be able to change the value of WORD, with just modifying one
      >>constant, rather than modifying all 3, as I am doing this on a much
      >>larger scale.[/color]
      >
      >
      > The C compiler concatenates any adjacent string literals into a single
      > string literal. Therefore
      > #define WORD_COLON WORD COLON
      > #define WORD_PERIOD WORD PERIOD
      > should do the trick.
      >[/color]
      you need the ## operator

      i.e.:

      #define concat(a, b) a ## b

      and if you call it with:

      concat(12,45), the pre-processor will replace it with 1245

      -yan

      Comment

      • Keith Thompson

        #4
        Re: Preprocessor define concatenation

        Yan <rottled@gmail. com> writes:[color=blue]
        > Joona I Palaste wrote:[color=green]
        >> michael.martin@ asahq.com scribbled the following:
        >>[color=darkred]
        >>>Hi,[/color]
        >>[color=darkred]
        >>>I am wondering if something like this is possible?[/color]
        >>[color=darkred]
        >>>#define COLON ":"
        >>>#define PERIOD "."[/color]
        >>[color=darkred]
        >>>#define WORD "word"[/color]
        >>[color=darkred]
        >>>#define WORD_COLON WORD+COLON
        >>>#define WORD_PERIOD WORD+PERIOD[/color]
        >>[color=darkred]
        >>>...[/color]
        >>[color=darkred]
        >>>I know the above is incorrect syntax. But is there a way to
        >>>concatenat e the above strings in some way? I would rather not have to
        >>>define WORD_COLON as "word:" and WORD_PERIOD as "word.". I would like
        >>>to be able to change the value of WORD, with just modifying one
        >>>constant, rather than modifying all 3, as I am doing this on a much
        >>>larger scale.[/color]
        >> The C compiler concatenates any adjacent string literals into a
        >> single
        >> string literal. Therefore
        >> #define WORD_COLON WORD COLON
        >> #define WORD_PERIOD WORD PERIOD
        >> should do the trick.
        >>[/color]
        > you need the ## operator
        >
        > i.e.:
        >
        > #define concat(a, b) a ## b
        >
        > and if you call it with:
        >
        > concat(12,45), the pre-processor will replace it with 1245[/color]

        The OP was trying to concatenate string literals. The ## operator is
        valid only if the result is a single token. concat(12,45) does yield
        the single token 1245, but concat("word"," :") yields "word"":", which
        is not a single token.

        Joona was correct. Implicit string literal concatenation will do what
        the OP wants; the ## operator (useful though it is for other things)
        will not.

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
        We must do something. This is something. Therefore, we must do this.

        Comment

        Working...