I've got a problem with C.
I'm trying to do something like the following:
#define FOOBAR 5 some_function_which_takes_an_integer (FOOBAR); ... some_function_which_takes_a_string ("String literal with FOOBAR concatenated: "FOOBAR);
Which I want to expand to:
some_function_which_takes_an_integer (5); ... some_function_which_takes_a_string ("String literal with FOOBAR concatenated: 5");
Unfortunately, I can't, and I can't see a way to do it. I'm basically looking for a way to stringise a preprocessor token's value so that it can be concatenated with other string literals before the compiler is allowed to molest it.
I've been talking with a friend, and seeing as there's a
# preprocessor operator which stringises token names, it would make sense that there would be one for token values, but he can't think of one, or any other way to do it apart from using
One other way of doing it would be to have two different versions of each preprocessor token – one an integer, and one a string – but that's ugly.
Anybody got any ideas?
Thank you to everyone who's replied. It turns out that I missed this page on GCC stringification which explains how to do it using only the following code:
#define xstr(s) str(s) #define str(s) #s
This produces the desired results when you use
Even better though, GLib has a macro for it already –
G_STRINGIFY – which uses the same solution as the GCC page details, but is included in GLib and wraps it nicely.