Expanding Macros into String Constants in C

Today I came across an annoying problem, how do I expand a C macro into a string?

One of C’s preprocessor operators is the # which surrounds the token that follows it in the replacement text with double quotes (“). So, at first the solution sounds pretty simple, just define

#define STR(tok) #tok

and things will work. However, there is one caveat: it will not work if passed another macro. For example,

#define BUF_LEN 100
#define STR(tok) #tok

STR(BUF_LEN)

will produce after going through the preprocessor

"BUF_LEN"

instead of "100", which is undesired. This behavior is due to the C standard noting that no macro expansions should happen to token preceded by #.

However, after reconsidering the source of the problem, I’ve found the following workaround: define another macro which will expand the argument and only then call the macro which does the quoting.

#define STR_EXPAND(tok) #tok
#define STR(tok) STR_EXPAND(tok)

#define BUF_LEN 100

STR(BUF_LEN)

will produce

"100"

as desired.

Explanation: The STR macro calls the STR_EXPAND macro with its argument. Unlike in the first example, this time the parameter is checked for macro expansions and evaluated by the preprocessor before being passed to STR_EXPAND which quotes it, thus giving the desired behavior.

13 thoughts on “Expanding Macros into String Constants in C”

  1. Awesome trick to get stringification working. I was looking for a solution like this to make portable printf() operand code (the %lu bits changes depending on architecture, and I needed to put a macro there instead).

  2. Exactly what I needed. Another thanks from the world out there for posting this.

  3. Thank you so much for this! I was already starting to pull out my hair when I stumbled upon this post.

  4. I can’t thank you enough for this “trick”! I’ve been so frustrated about my self generated compiler messages that I couldn’t print values with during the build. I wrote a short python program to extract all my #defines, and create a file with #pragma message lines for all of the symbols, using this trick! This way I can emit all my defined values during the compile!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.