4

Possible Duplicate:
Macro for concatenating two strings in C

How to concatenate two strings with a macro?

I tried this but it does not give correct results:

#define CONCAT(string) "start"##string##"end"
Community
  • 1
  • 1
MOHAMED
  • 38,769
  • 51
  • 148
  • 252

1 Answers1

10

You need to omit the ##: adjacent string literals get concatenated automatically, so this macro is going to concatenate the strings the way you want:

#define CONCAT(string) "start"string"end"

For two strings:

#define CONCAT(a, b) (a"" b)

Here is a link to a demo on ideone.

Jared Burrows
  • 52,770
  • 23
  • 148
  • 184
Sergey Kalinichenko
  • 697,062
  • 78
  • 1,055
  • 1,465