ANSI CPP string concatenation

Peter Holzer hp at vmars.tuwien.ac.at
Fri Jun 21 01:47:58 AEST 1991


nigel at 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 at vmars.tuwien.ac.at                 |     Tony Rand |



More information about the Comp.lang.c mailing list