Stringification - Red Hat ENTERPRISE LINUX 3 - USING CPP Using Instructions

Using cpp, the c preprocessor
Hide thumbs Also See for ENTERPRISE LINUX 3 - USING CPP:
Table of Contents

Advertisement

Chapter 3. Macros
17
Whitespace is not a preprocessing token, so if a macro
takes one argument,
and
foo
foo ()
foo (
both supply it an empty argument. Previous GNU preprocessor implementations and documentation
)
were incorrect on this point, insisting that a function-like macro that takes a single argument be passed
a space if an empty argument was required.
Macro parameters appearing inside string literals are not replaced by their corresponding actual argu-
ments.
#define foo(x) x, "x"
foo(bar)
==> bar, "x"
3.4. Stringification
Sometimes you may want to convert a macro argument into a string constant. Parameters are not
replaced inside string constants, but you can use the
preprocessing operator instead. When a macro
#
parameter is used with a leading
, the preprocessor replaces it with the literal text of the actual
#
argument, converted to a string constant. Unlike normal parameter replacement, the argument is not
macro-expanded first. This is called stringification.
There is no way to combine an argument with surrounding text and stringify it all together. Instead,
you can write a series of adjacent string constants and stringified arguments. The preprocessor will
replace the stringified arguments with string constants. The C compiler will then combine all the
adjacent string constants into one long string.
Here is an example of a macro definition that uses stringification:
#define WARN_IF(EXP) \
do { if (EXP) \
fprintf (stderr, "Warning: " #EXP "\n"); } \
while (0)
WARN_IF (x == 0);
==> do { if (x == 0)
fprintf (stderr, "Warning: " "x == 0" "\n"); } while (0);
The argument for
is substituted once, as-is, into the
statement, and once, stringified, into the
EXP
if
argument to
. If
were a macro, it would be expanded in the
statement, but not in the
fprintf
x
if
string.
The
and
are a kludge to make it possible to write
, which the resem-
do
while (0)
WARN_IF (
);
arg
blance of
to a function would make C programmers want to do; see Section 3.9.3 Swallowing
WARN_IF
the Semicolon.
Stringification in C involves more than putting double-quote characters around the fragment. The pre-
processor backslash-escapes the quotes surrounding embedded string constants, and all backslashes
within string and character constants, in order to get a valid C string constant with the proper con-
tents. Thus, stringifying
results in "p = \"foo\\n\";". However, backslashes that are
p = "foo\n";
not inside string or character constants are not duplicated:
by itself stringifies to "\n".
\n
All leading and trailing whitespace in text being stringified is ignored. Any sequence of whitespace in
the middle of the text is converted to a single space in the stringified result. Comments are replaced
by whitespace long before stringification happens, so they never appear in stringified text.
There is no way to convert a macro argument into a character constant.

Advertisement

Table of Contents
loading

This manual is also suitable for:

Enterprise linux 3

Table of Contents