473,408 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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

Nov 14 '05 #1
3 25952
mi************@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 (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"We're women. We've got double standards to live up to."
- Ally McBeal
Nov 14 '05 #2
Yan
Joona I Palaste wrote:
mi************@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
Nov 14 '05 #3
Yan <ro*****@gmail.com> writes:
Joona I Palaste wrote:
mi************@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) ks***@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.
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

97
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
4
by: Jakob Simon-Gaarde | last post by:
Some project includes files from different libraries lib1,lib2 and lib3 all having each there own version header file. I need to be able to pick up these values in a single define value...
1
by: Cronus | last post by:
Hi the following code the g++ (g++ (GCC) 3.3.3 (Debian 20040422)) emits the error message that pasting of :: and hello is no valid preprocessor token. The g++ 2.95.3 accepts the code. I know...
9
by: pozz | last post by:
Hi all, I have the below #defines #define NUMBER1 30 #define NUMBER2 50 #define SUM (NUMBER1+NUMBER2) #define STRING1 "Byte: \x30" #define STRING2 "Byte: \x50"...
1
by: Harold Howe | last post by:
Is there a preprocessor define that identifies which version of the compiler is processing a source file? Something analogous to _MSC_VER, __BORLANDC__ and __GNUC__. H^2
1
by: ee02iu | last post by:
I was told C preprocessor "#define arg1 agr2" would do a text replacement - arg1 string with arg2.... I wrote a few lines of code as follows: #include "stdio.h" #define PRNT(v,x) printf("x...
31
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes...
1
by: Guenter Dannoritzer | last post by:
Hi, I have a piece of code that got developed using Microsoft .NET 2003 and I try to compile it with g++/gcc 4.1.2. There is a problem with the string concatenation operator for the...
4
by: Ivan | last post by:
Hi all, What is the scope of preprocessor #define? Does it depends on the compiler? Thanks Ivan
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.