Macros

How to Stringify a Macro in C++

Learn how the # operator turns a C++ macro argument into a string literal, with examples, the two-level trick for expanded macros and common pitfalls.

On this page
  1. What the # operator does
  2. Printing a variable’s name and value
  3. Why a second macro helps
  4. Common pitfalls
  5. Where stringification is used

To stringify a macro argument, put a # in front of the parameter name in the body of a function-like macro. The preprocessor then turns that argument into a string literal before the compiler reads the code.

#define STRINGIFY(x) #x

const char* name = STRINGIFY(hello);  // "hello"

What the # operator does

The # operator works only in the body of a function-like macro, and it must be followed by a parameter name. The preprocessor takes the tokens of the argument and wraps them in double quotes. This happens before compilation, so the string is fixed when the program is built.

Spacing is normalized. Leading and trailing whitespace is dropped, and each run of whitespace between tokens becomes one space. Spaces you did not write are not added:

#define STRINGIFY(x) #x

STRINGIFY(hello)              // "hello"
STRINGIFY(a+b)                // "a+b"
STRINGIFY(a + b)              // "a + b"
STRINGIFY(   a   +   b   )    // "a + b"

The result is an ordinary string literal, so it fits anywhere a literal does. To store it in a std::string, include <string> and assign it as usual: std::string name = STRINGIFY(hello);.

Do not confuse # with ##. The ## operator joins two tokens into one instead of making a string. It is a separate feature and is not covered here.

Printing a variable’s name and value

A common use is a small debug helper that prints both the name and the value of a variable:

#include <iostream>

#define PRINT_VAR(var) std::cout << #var << " = " << (var) << '\n'

int main() {
    int count = 42;
    PRINT_VAR(count);  // prints: count = 42
}

#var becomes the string "count", and (var) prints the value. The parentheses around var keep an expression such as PRINT_VAR(a + b) evaluating as one unit.

Why a second macro helps

A parameter used with # is not expanded first. So if the argument is the name of another macro, you get the name rather than its value:

#define STR(x) #x

#define VERSION 3

STR(VERSION)  // "VERSION"

To get the value, pass the argument through a second macro. This wrapper uses its parameter without #, so the argument is expanded before it reaches the macro that does the stringifying:

#define STR(x) #x
#define XSTR(x) STR(x)

#define VERSION 3

STR(VERSION)   // "VERSION"
XSTR(VERSION)  // "3"

The same pattern works for __LINE__. XSTR(__LINE__) gives the current line number as a string. Adjacent string literals join into one, so "Line " XSTR(__LINE__) produces a single string such as "Line 42".

Common pitfalls

  • Only parameters. In a function-like macro, # must be followed by a parameter name. A body such as #hello is a compile error.
  • Quotes and backslashes are escaped. An argument of "hi" becomes the four-character text "hi", quote marks included. A backslash in the argument is escaped the same way.
  • The value is fixed at build time. The string is the spelling in the source, not a value computed while the program runs. To convert a number at run time, use a function such as std::to_string.

Where stringification is used

Stringification is most useful for debug output, assertion messages and logging helpers. It is one of several preprocessor features, so if macros are new to you, start with “What are macros in C code?” Because macros carry risks of their own, read “Why not use macros in C++?” before you use them widely.

Keep exploring

Guides from other topics

All topics →