Skip to main content
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.

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.

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.
Tired of Poor C Skills? Fix That With Deep Grasping!Learn More