Skip to content


C conundrum (below)

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.

Posted in General. Tagged with , , , .

11 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Ryan said

    If you know the type ahead of time, you could probably use sprintf.
    http://www.cppreference.com/stdio/sprintf.html

  2. Noname said

    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));

  3. Benjamin Otte said

    G_STRINGIFY does exactly that for me

  4. Christian Vogel said

    have a look at the bottom of http://gcc.gnu.org/onlinedocs/cpp/Stringification.html

  5. tuXXX said

    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 :)

  6. ion said

    #define FOOBAR(n) do { foo (n); bar ("baz: " #n); } while (0)

    FOOBAR (1+2) will call foo (3) and bar ("baz: 1+2").

  7. ion said

    Sorry, please ignore my previous comment. I misparsed the question due to my browser not rendering the code with any line feeds.

  8. Philip said

    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.

  9. Zack said

    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));

  10. me said

    If you want to do people a favor, update your blog post with the solution you chose (or post another one)