Preprocessor define concatenation

Discussion in 'C Programming' started by michael.martin@asahq.com, Jan 7, 2005.

  1. Guest

    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
     
    , Jan 7, 2005
    #1
    1. Advertising

  2. scribbled the following:
    > 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.


    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 () ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "We're women. We've got double standards to live up to."
    - Ally McBeal
     
    Joona I Palaste, Jan 7, 2005
    #2
    1. Advertising

  3. Yan Guest

    Joona I Palaste wrote:
    > scribbled the following:
    >
    >>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.

    >
    >
    > 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.
    >

    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
     
    Yan, Jan 8, 2005
    #3
  4. Yan <> writes:
    > Joona I Palaste wrote:
    >> scribbled the following:
    >>
    >>>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.

    >> 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.
    >>

    > 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


    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_Keith) <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.
     
    Keith Thompson, Jan 8, 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. walala
    Replies:
    3
    Views:
    4,835
    walala
    Sep 18, 2003
  2. Cronus
    Replies:
    1
    Views:
    695
    Paul Mensonides
    Jul 15, 2004
  3. recvfrom
    Replies:
    1
    Views:
    300
    A. Sinan Unur
    Mar 20, 2006
  4. Replies:
    3
    Views:
    304
  5. mrstephengross
    Replies:
    8
    Views:
    681
    Jack Klein
    Oct 12, 2005
Loading...

Share This Page