PATH
"Stringification" means turning a code fragment into a string constant whose contents are the text for the code fragment. For example, stringifying foo (z) results in "foo (z)" .
In the C preprocessor, stringification is an option available when macro arguments are substituted into the macro definition. In the body of the definition, when an argument name appears, the character # before the name specifies stringification of the corresponding argument when it's substituted at that point in the definition. The same argument may be substituted in other places in the definition without stringification if the argument name appears in those places with no # .
Here's an example of a macro definition that uses stringification:
#define WARN_IF(EXP) \
do { if (EXP) fprintf(stderr, "Warning: " #EXP "\n"); } while(0)
Here the argument for EXP is substituted once as given, into the if statement, and once as stringified, into the argument to fprintf . The do and while (0) make it possible to write WARN_IF (ARG); safely (see the section " See Swallowing the Semicolon ").
The stringification feature is limited to transforming one macro argument into one string constant: There's no way to combine the argument with other text and then stringify it all together. But the example above shows how an equivalent result can be obtained in ANSI-standard C using the feature that adjacent string constants are concatenated as one string constant. The preprocessor stringifies the actual value of EXP into a separate string constant, resulting in text like
do { if (x==0) fprintf (stderr, "Warning: " "x == 0" "\n"); } while(0)
but the C compiler then sees three consecutive string constants and concatenates them into one, producing:
do { if (x==0) fprintf (stderr, "Warning: x == 0\n"); } while (0)
Stringification in C involves more than putting double quotes around the fragment; it's necessary to put backslashes in front of all double quotes, and all backslashes in string and character constants, in order to get a valid C string constant with the proper contents. Thus, stringifying p = "foo\n"; results in "p = \"foo\\n\";" . However, backslashes that aren't inside string or character constants aren't duplicated: \n by itself stringifies to "\n" .
White-space characters (including comments) in the text being stringified are handled according to the following rules:
It's often useful to define, for example:
STR(X) #X
so that when you use the macro instead of #X directly, X is re-scanned one more time for macro expansion.