Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Preprocessor define concatenation (http://www.velocityreviews.com/forums/t436433-preprocessor-define-concatenation.html)

michael.martin@asahq.com 01-07-2005 02:24 PM

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 01-07-2005 02:27 PM

Re: Preprocessor define concatenation
 
michael.martin@asahq.com 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 (palaste@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"We're women. We've got double standards to live up to."
- Ally McBeal

Yan 01-08-2005 12:08 AM

Re: Preprocessor define concatenation
 
Joona I Palaste wrote:
> michael.martin@asahq.com 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

Keith Thompson 01-08-2005 12:36 AM

Re: Preprocessor define concatenation
 
Yan <rottled@gmail.com> writes:
> Joona I Palaste wrote:
>> michael.martin@asahq.com 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) 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.


All times are GMT. The time now is 09:31 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.