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 sprintf
.
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 xstr(FOOBAR)
.
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.
If you know the type ahead of time, you could probably use sprintf.
http://www.cppreference.com/stdio/sprintf.html
Try the following:
#define FOOBAR 5
#define STRINGIFY(x) #x
#define STRINGIFYVALUE(x) STRINGIFY(x)
some_function_which_takes_a_string ("String literal with FOOBAR concatenated: "STRINGIFY(FOOBAR));
some_function_which_takes_a_string ("String literal with FOOBAR concatenated: "STRINGIFYVALUE(FOOBAR));
G_STRINGIFY does exactly that for me
have a look at the bottom of http://gcc.gnu.org/onlinedocs/cpp/Stringification.html
This is described in the gcc C preprocessor documentation ( http://gcc.gnu.org/onlinedocs/cpp/Stringification.html ) :
#define xstr(s) str(s)
#define str(s) #s
#define FOOBAR 5
[...]
some_function_which_takes_an_integer (FOOBAR);
some_function_which_takes_a_string ("String literal with FOOBAR concatenated: " xstr(FOOBAR));
After preprocessing, this will be :
some_function_which_takes_an_integer (5);
some_function_which_takes_a_string ("String literal with FOOBAR concatenated: " "5");
And then the compiler will concatenate the two strings as :
some_function_which_takes_a_string ("String literal with FOOBAR concatenated: 5");
Hope it'll help you
#define FOOBAR(n) do { foo (n); bar ("baz: " #n); } while (0)
FOOBAR (1+2)
will callfoo (3)
andbar ("baz: 1+2")
.Sorry, please ignore my previous comment. I misparsed the question due to my browser not rendering the code with any line feeds.
Thanks everyone. Benjamin's suggestion is the most useful, since what I'm working on uses Glib, but I'm grateful for others' suggestions too.
I feel a bit of an idiot now, as the solution's so simple. Live and learn.
I am not sure I understand your example but I believe this is the construct you want:
#define S(x) S_(x)
#define S_(x) #x
...
some_function_which_takes_a_string ("String literal with FOOBAR concatenated: " S(FOOBAR));
http://gcc.gnu.org/onlinedocs/gcc-4.2.2/cpp/Concatenation.html#Concatenation
If you want to do people a favor, update your blog post with the solution you chose (or post another one)