[comp.lang.c] ANSI CPP string concatenation

nigel@cnw01.storesys.coles.oz.au (Nigel Harwood) (06/19/91)

Does anyone know if you can append strings in ANSI C.

For example I want a macro which appends one of its numeric
arguments to a string coded into the marco.

#define	APPEND(x,v)	{ donothing+x = v; }

I know that the '+' isn't what I'm after but using whatever the method
turns out to be what I want it to generate from APPEND(1,123) is:

	{ donothing1 = 123; }

Anyone know the secret ?

Regards
-- 
<<<<<<<<<<<<<<<<<<<<<<<<<  Nigel Harwood  >>>>>>>>>>>>>>>>>>>>>>>>>>>
<< Post:  Coles Myer Ltd, PO Box 2000 Tooronga 3146, Australia     >>
<< Phone: +61 3 829 6090  E-mail: nigel@cnw01.storesys.coles.oz.au >>
<<   FAX: +61 3 829 6886                                           >>

pckim@unisql.UUCP (Pyung-Chul Kim) (06/20/91)

In article <1271@cnw01.storesys.coles.oz.au> nigel@cnw01.storesys.coles.oz.au (Nigel Harwood) writes:
>Does anyone know if you can append strings in ANSI C.
>
>For example I want a macro which appends one of its numeric
>arguments to a string coded into the marco.
>
>#define	APPEND(x,v)	{ donothing+x = v; }
>
>I know that the '+' isn't what I'm after but using whatever the method
>turns out to be what I want it to generate from APPEND(1,123) is:
>
>	{ donothing1 = 123; }
>

FAQ 37. says that we can use ## for this purpose.
	37.  I have some old code that tries to construct identifiers with a
	     macro like "#define Paste(a, b) a/**/b", but it doesn't work any
	     more.

	     A:   Try the ANSI token-pasting operator ##.
That is,

#define	APPEND(x,v)	{ donothing ## x = v; }

Hope it helps


-- 
Pyung-Chul Kim

UniSQL, Inc.
9390 Research Blvd., Kaleido II, Suite 220, Austin, TX 78759
Internet: execu!sequoia!unisql!pckim@cs.utexas.edu
UUCP: {uunet, cs.utexas.edu!execu}!sequoia!unisql!pckim
TEL: (512)343-7297 Ext. 332
FAX: (512)343-7383

hp@vmars.tuwien.ac.at (Peter Holzer) (06/20/91)

nigel@cnw01.storesys.coles.oz.au (Nigel Harwood) writes:

>Does anyone know if you can append strings in ANSI C.

>For example I want a macro which appends one of its numeric
>arguments to a string coded into the marco.

>#define	APPEND(x,v)	{ donothing+x = v; }

Nitpick: I do not see any strings here, just tokens (a string is
something between double quotes)

>I know that the '+' isn't what I'm after but using whatever the method
>turns out to be what I want it to generate from APPEND(1,123) is:

>	{ donothing1 = 123; }

>Anyone know the secret ?

Yes. The token pasting operator is ##. So your macro should read:

#define APPEND(x,v)	{donothing ## x = v;}

--
|    _  | Peter J. Holzer                       | Think of it   |
| |_|_) | Technical University Vienna           | as evolution  |
| | |   | Dept. for Real-Time Systems           | in action!    |
| __/   | hp@vmars.tuwien.ac.at                 |     Tony Rand |

fischer@iesd.auc.dk (Lars P. Fischer) (06/21/91)

>>>>> On 19 Jun 91, nigel@cnw01.storesys.coles.oz.au (Nigel Harwood) said:

Nigel> Does anyone know if you can append strings in ANSI C.

"The preprocessor operator ## provides a way to concatenate actual
 arguments during macro expansion. [...]

   #define paste(front, back) front ## back"

	- Kernighan & Ritchie: The C Programming Language, 2.ed, p.90

Found by looking up "concatenate" in the index.

RTFM.

/Lars
--
Lars Fischer,  fischer@iesd.auc.dk   | It takes an uncommon mind to think of
CS Dept., Univ. of Aalborg, DENMARK. | these things.  -- Calvin

bruno@sdcc10.ucsd.edu (Bruce W. Mohler) (06/21/91)

In article <FISCHER.91Jun20221733@galilei.iesd.auc.dk> fischer@iesd.auc.dk (Lars P. Fischer) writes:
 >>>>>> On 19 Jun 91, nigel@cnw01.storesys.coles.oz.au (Nigel Harwood) said:
 >
 >Nigel> Does anyone know if you can append strings in ANSI C.
 >
 >"The preprocessor operator ## provides a way to concatenate actual
 > arguments during macro expansion. [...]
 >
 >   #define paste(front, back) front ## back"
 >
 >	- Kernighan & Ritchie: The C Programming Language, 2.ed, p.90
 >
 >Found by looking up "concatenate" in the index.
 >
 >RTFM.
 >
 >/Lars
 >--
 >Lars Fischer,  fischer@iesd.auc.dk   | It takes an uncommon mind to think of
 >CS Dept., Univ. of Aalborg, DENMARK. | these things.  -- Calvin

What options do I have if my preprocessor does not support "##"?
I've tried having the 2 strings sandwiched between empty comments
("/**/").

Any suggestions?

Bruce

-- 
Bruce W. Mohler
Systems Programmer (aka Staff Analyst)
bruno@sdcc10.ucsd.edu
voice: 619-586-2218

Dave.Harris@f14.n15.z1.fidonet.org (Dave Harris) (06/22/91)

 >From: nigel@cnw01.storesys.coles.oz.au (Nigel Harwood)
 >Does anyone know if you can append strings in ANSI C.

 >For example I want a macro which appends one of its numeric
 >arguments to a string coded into the marco.

 >#define APPEND(x,v)     { donothing+x = v; }

 >I know that the '+' isn't what I'm after but using whatever the method
 >turns out to be what I want it to generate from APPEND(1,123) is:

 >        { donothing1 = 123; }

 >Anyone know the secret ?

The secret is to use an array (like donothing[5]) or a lookup table.  This 
would be considered a better style unless you are using this differently than 
I think you are trying to use this....

but...  Mine is not the place to ask.

#define APPEND(x,v)   donothing##x = v
 


 

--  
Uucp: ...{gatech,ames,rutgers}!ncar!asuvax!stjhmc!15!14!Dave.Harris
Internet: Dave.Harris@f14.n15.z1.fidonet.org

sarima@tdatirv.UUCP (Stanley Friesen) (06/25/91)

In article <20832@sdcc6.ucsd.edu> bruno@sdcc10.ucsd.edu (Bruce W. Mohler) writes:
>What options do I have if my preprocessor does not support "##"?
>I've tried having the 2 strings sandwiched between empty comments
>("/**/").
>
>Any suggestions?

What preprocessor do you have?

Seriously, every non-ANSI preprocessor known does this differently ( at
least for all practical purposes).

Once we know the vendor, version and OS in question, we *might* be able
to give you an answer.
-- 
---------------
uunet!tdatirv!sarima				(Stanley Friesen)