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?

3 Answers 3

50

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'
Sign up to request clarification or add additional context in comments.

5 Comments

Keep in mind this is not necessarily dumb if you're doing something like dumb_macro(mypackage,myfunction)...
@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... :)
whoa that's one great answer. Wanted to know how to do that symbol concatenation. It's really useful..
@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; 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 two examples, I hope they can help you, good luck!
5
#define space_conc(str1, str2) #str1 " " #str2
printf("%s", space_conc(hello, you)); // Will print "hello you"

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.