473,394 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Macro concatenation

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.
Mar 28 '07 #1
5 3641
On Wed, 28 Mar 2007 13:03:39 +0000, K. Jennings wrote:
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;
}

Mar 28 '07 #2
On Wed, 28 Mar 2007 14:56:39 +0100, Duncan Muirhead wrote:
On Wed, 28 Mar 2007 13:03:39 +0000, K. Jennings wrote:
> 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;
}
Thanks. However, this does not work if the argument to
FUNCTION_NAME is itself a macro. That's essentially what I am looking for.
Mar 28 '07 #3
On Mar 28, 9:48 am, "K. Jennings" <kjenni...@resurgence.netwrote:
On Wed, 28 Mar 2007 14:56:39 +0100, Duncan Muirhead wrote:
[...]
#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
Mar 28 '07 #4
K. Jennings wrote:
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) ;

--
Thad
Mar 29 '07 #5
Thad Smith <ThadSm...@acm.orgwrote:
K. Jennings wrote:
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) ;
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;
}

--
Peter

Mar 29 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jonas Galvez | last post by:
Is it true that joining the string elements of a list is faster than concatenating them via the '+' operator? "".join() vs 'a'+'b'+'c' If so, can anyone explain why?
8
by: Siemel Naran | last post by:
#define EXPECT_ASSERT(x) { if (!x) expect_assert(localVariable, __FILE__, __LINE__, #x); } MSVC7 gives an error: "error C2014: preprocessor command must start as first nonwhite space".
3
by: John | last post by:
Please, consider this macro: #define mymacro(arg1, arg2) arg1 and arg2 Then it is used: mymacro(boys, girls) How is its expansion?
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
3
by: boaring | last post by:
I need to use Macro's in Excel and always the same changes to 31 Sheets. (31 days in the Month). I'm trying to use a FOR loop but cannot get the correct format to have the sheet number to increment...
1
by: somenath | last post by:
Hi ALL , I need to print the name of the function as my debug message. I have tried the following way #include<stdio.h> #include<stdlib.h> #define __STR(x) _VAL(x) #define _VAL(x) #x int...
5
by: Francois Grieu | last post by:
One of the C compiler that I use <OT>(Keil's CX51)</OTbarks at #define a(b) b int main(void){return a( #if 0 #endif 0);} More generally, this compiler seems confused by any preprocessing...
4
by: cppcraze | last post by:
Hi, I am just stumbled by a problem about concatenation in macro. See below code snippet: // there're some contants definition in this class struct X { enum {A, B, C}; };
34
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.