Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a string from a MACRO function in C?

I want to create a string based on the value passed as argument to the MACRO FUNCTION. Something Like:

#define ABC(x,y) return "anystr_x_y.tar.bz2"
main()
{
   a = ABC(2,3);
}

So Finally, It should return "anystr_2_3.tar.bz2"

I was wondering how to create that string with the value passed as argument to MACRO fnc.

Any help ! thanks !

like image 596
mujahid Avatar asked Oct 20 '25 23:10

mujahid


2 Answers

Define your macro like this, using the "stringize operator":

#define ABC(x,y) "anystr_" #x "_" #y ".tar.bz2"
like image 173
cnicutar Avatar answered Oct 22 '25 16:10

cnicutar


Macros don't return stuff, since they are not functions. They are merely token replacement functionality. What you actually want is a though one, and in your place I would do it with a function instead. However, a solution could look like:

#define ABC(x,y) "anystr_" #x "_" #y ".tar.bz2"

This takes leverage from the fact that string literals are collapsed togheter, so "Hello " "World!" is interpreted as "Hello World!".

like image 32
K-ballo Avatar answered Oct 22 '25 15:10

K-ballo