Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

Macro stringification is a C preprocessor mechanism that converts a function-like macro parameter into a string literal. This is achieved using the stringification operator (#), which instructs the preprocessor to enclose the exact text of the provided macro argument in double quotes during the macro expansion phase.

Syntax

The # operator must precede the parameter name in the macro’s replacement list. The # and the parameter name are evaluated as adjacent preprocessing tokens, meaning whitespace between them is perfectly valid and ignored by the preprocessor.
#define STRINGIFY(param) #param
#define STRINGIFY_WITH_SPACE(param) # param

Preprocessor Mechanics

When the preprocessor encounters the # operator, it applies specific lexical rules to the argument tokens before generating the string literal:
  1. Literal Conversion: The raw text of the argument is wrapped in double quotes (").
  2. Whitespace Normalization: Leading and trailing whitespace around the argument is discarded. Multiple consecutive whitespace characters between tokens within the argument are collapsed into a single space.
  3. Automatic Escaping: The preprocessor automatically inserts escape characters (\) for double quotes (") and backslashes (\) strictly when those characters are already part of a string literal or a character constant token within the argument. Stray backslashes outside of these specific tokens are not escaped.
#define TO_STRING(x) # x

// 1. Literal conversion
TO_STRING(hello) 
// Expands to: "hello"

// 2. Whitespace normalization
TO_STRING(  int   a  =   5;  ) 
// Expands to: "int a = 5;"

// 3. Automatic escaping (String literals)
TO_STRING(printf("Hello\n");) 
// Expands to: "printf(\"Hello\\n\");"

// 3. Automatic escaping (Character constants)
TO_STRING('\n')
// Expands to: "'\\n'"

// 3. No escaping for stray backslashes outside literals/constants
TO_STRING(\n)
// Expands to: "\n"

Evaluation Order and Indirection

A critical mechanical behavior of the # operator is that it suppresses the macro expansion of its operand. If the argument passed to the stringified parameter is itself a macro, the preprocessor will stringify the macro’s identifier, not its expanded value. To stringify the result of an expanded macro, a two-level macro architecture (indirection) is required. This forces the preprocessor to evaluate the inner macro before applying the stringification operator.
#define VERSION 17

// Level 1: Applies the # operator (suppresses expansion)
#define STR(x) #x

// Level 2: Forces argument expansion before passing to Level 1
#define XSTR(x) STR(x)

// Direct stringification: Argument is NOT expanded
STR(VERSION)  
// Expands to: "VERSION"

// Indirect stringification: Argument IS expanded first
XSTR(VERSION) 
// Step 1: XSTR(17)
// Step 2: STR(17)
// Expands to: "17"
Master C with Deep Grasping Methodology!Learn More