22

I'm trying to define a macro which is suppose to take 2 string values and return them concatenated with a one space between them. It seems I can use any character I want besides space, for example:

#define conc(str1,str2) #str1 ## #str2 
#define space_conc(str1,str2) conc(str1,-) ## #str2

space_conc(idan,oop);

space_conc would return "idan-oop"

I want something to return "idan oop", suggestions?

a3f
  • 8,517
  • 1
  • 41
  • 46
Idan
  • 5,717
  • 10
  • 47
  • 84

3 Answers3

49

Try this

#define space_conc(str1,str2) #str1 " " #str2

The '##' is used to concatenate symbols, not strings. Strings can simply be juxtaposed in C, and the compiler will concatenate them, which is what this macro does. First turns str1 and str2 into strings (let's say "hello" and "world" if you use it like this space_conc(hello, world)) and places them next to each other with the simple, single-space, string inbetween. That is, the resulting expansion would be interpreted by the compiler like this

"hello" " " "world"

which it'll concatenate to

"hello world"

HTH

Edit
For completeness, the '##' operator in macro expansion is used like this, let's say you have

#define dumb_macro(a,b) a ## b

will result in the following if called as dumb_macro(hello, world)

helloworld

which is not a string, but a symbol and you'll probably end up with an undefined symbol error saying 'helloworld' doesn't exist unless you define it first. This would be legal:

int helloworld;
dumb_macro(hello, world) = 3;
printf ("helloworld = %d\n", helloworld); // <-- would print 'helloworld = 3'
falstro
  • 34,597
  • 9
  • 72
  • 86
  • 2
    Keep in mind this is not necessarily dumb if you're doing something like `dumb_macro(mypackage,myfunction)`... – R.. GitHub STOP HELPING ICE Jan 09 '11 at 20:23
  • @R., of course the concept has uses, but it's generally connected to some other semantic value, or the option to replace the macro with something else (such as, for debugging purposes). I didn't want to dive in to anything like that, so this particular one is just a dumb macro concatenating two symbol names for no real purpose... :) – falstro Jan 10 '11 at 10:05
  • whoa that's one great answer. Wanted to know how to do that symbol concatenation. It's really useful.. – Anubis Jan 07 '13 at 11:44
  • @roe I have a thing to ask. `dumb_macro(a,b) a ## b` will concatenate the exact symbols, as you've shown. But how can i concat a symbol with a value of a variable? Say there an integer `n` which hold a value 1,2... How can i concatenate `sym` with the **value of** n (not with `n`)? for eg. if n=1, then i need to get `sym2`. – Anubis Jan 07 '13 at 11:57
  • @Anubis; Due to some quirks in the preprocessing spec, it'll only recursively evaluate tokens if they're not part of a concatenation or made into strings, so you'll need a level of indirection. Quick google gives [these](http://stackoverflow.com/questions/1597007/creating-c-macro-with-and-line-token-concatenation-with-positioning-macr) two [examples](http://stackoverflow.com/questions/4284733/preprocessor-token-expansion), I hope they can help you, good luck! – falstro Jan 08 '13 at 08:05
5
#define space_conc(str1, str2) #str1 " " #str2
printf("%s", space_conc(hello, you)); // Will print "hello you"
Andreas Bonini
  • 44,018
  • 30
  • 122
  • 156
1

The right was to do it is to place the 2 strings one next to the other. '##' won't work. Just:

#define concatenatedstring string1 string2
Michael Haephrati
  • 3,660
  • 1
  • 33
  • 56