Macro concatenation

K

K. Jennings

I have a function whose name should be composed of a prefix, an
underscore character and a suffix, like

xxx_f

What I would like to do is for the prefix to be fixed in a preprocessor
macro, so that the full name can be constructed later on:

#define MY_PREFIX xxx

#define FUNCTION_NAME MY_PREFIX_f

void FUNCTION_NAME(int a) ;

Of course, this does not work; in the end, the resulting function
prototype is

void MY_PREFIX_f(int a) ;

Can this be done at all, using the C preprocessor alone?

I tried with the ## operator to no avail. Alternatively, if one
defines

#define FUNCTION_NAME (MY_PREFIX)_f

then one obtains

void (xxx)_f(int a) ;

close, but no cigar.
 
D

Duncan Muirhead

I have a function whose name should be composed of a prefix, an
underscore character and a suffix, like

xxx_f

What I would like to do is for the prefix to be fixed in a preprocessor
macro, so that the full name can be constructed later on:
<snip>
The preprocessor output from
#include <stdlib.h>
#include <stdio.h>
#define FUNCTION_NAME( name) xyz_ ## name
int FUNCTION_NAME( bob)( int a)
{ return a;
}
int main( int argc, char** argv)
{ printf( "%d\n", FUNCTION_NAME( bob)(17));
return EXIT_SUCCESS;
}

is
int xyz_bob( int a)
{ return a;
}
int main( int argc, char** argv)
{ printf( "%d\n", xyz_bob(17));
return 0;
}
 
K

K. Jennings

<snip>
The preprocessor output from
#include <stdlib.h>
#include <stdio.h>
#define FUNCTION_NAME( name) xyz_ ## name int FUNCTION_NAME ( bob)( int
a)
{ return a;
}
int main( int argc, char** argv)
{ printf( "%d\n", FUNCTION_NAME( bob)(17));
return EXIT_SUCCESS;
}

is
int xyz_bob( int a)
{ return a;
}
int main( int argc, char** argv)
{ printf( "%d\n", xyz_bob(17));
return 0;
}

Thanks. However, this does not work if the argument to
FUNCTION_NAME is itself a macro. That's essentially what I am looking for.
 
D

Dave Hansen

#define FUNCTION_NAME( name) xyz_ ## name
int FUNCTION_NAME( bob)( int a)
{ return a;
}
[...]
Thanks. However, this does not work if the argument to
FUNCTION_NAME is itself a macro. That's essentially what I am looking for.

Your question could be clearer. Perhaps you meant something like

#define MY_NAME bob
#define MY_PREFIX xyz
#define EXPAND_FN(p,n) p ## _ ## n
#define FUNCTION_NAME(p,n) EXPAND_FN(p,n)

int FUNCTION_NAME(MY_PREFIX, MY_NAME) (int a) <etc.>

Untested code. But it should give you a start.

-=Dave

#define FUNCTION
 
P

Peter Nilsson

Thad Smith said:
#define MY_PREFIX xxx
#define JOIN_MACROS(a,b) a ## b
#define FUNCTION_NAME JOIN_MACROS(MY_PREFIX, _f)
void FUNCTION_NAME(int a) ;

The OP wanted xxx_f, not MY_PREFIX_f.

Try...

#include <stdio.h>

#define CAT(X,Y) X ## Y
#define CAT2(X,Y) CAT(X,Y)
#define CAT3(X,Y,Z) CAT2(X,CAT2(Y,Z))
#define CAT4(X,Y,Z,A) CAT2(X,CAT3(Y,Z,A))
/* ... */

#define STR(X) #X
#define STRSTR(X) STR(X)

#if 0
#define MY_PREFIX xxx
#define FUNCTION_NAME CAT2(MY_PREFIX, _f)
#else
#define MY_PREFIX xxx
#define MY_SUFFIX f
#define FUNCTION_NAME CAT3(MY_PREFIX, _, MY_SUFFIX)
#endif

void FUNCTION_NAME(const char *msg)
{
printf("%s says: %s\n", STRSTR(FUNCTION_NAME), msg);
}

int main(void)
{
FUNCTION_NAME("Hello World");
return 0;
}
 
T

Thad Smith

K. Jennings said:
I have a function whose name should be composed of a prefix, an
underscore character and a suffix, like

xxx_f

What I would like to do is for the prefix to be fixed in a preprocessor
macro, so that the full name can be constructed later on:

#define MY_PREFIX xxx

#define FUNCTION_NAME MY_PREFIX_f

void FUNCTION_NAME(int a) ;

Of course, this does not work; in the end, the resulting function
prototype is

void MY_PREFIX_f(int a) ;

Can this be done at all, using the C preprocessor alone?

#define MY_PREFIX xxx
#define JOIN_MACROS(a,b) a ## b
#define FUNCTION_NAME JOIN_MACROS(MY_PREFIX, _f)
void FUNCTION_NAME(int a) ;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top