• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/toolchains/hndtools-armeabi-2011.09/share/doc/arm-arm-none-eabi/html/cpp/
1<html lang="en">
2<head>
3<title>Stringification - The C Preprocessor</title>
4<meta http-equiv="Content-Type" content="text/html">
5<meta name="description" content="The C Preprocessor">
6<meta name="generator" content="makeinfo 4.13">
7<link title="Top" rel="start" href="index.html#Top">
8<link rel="up" href="Macros.html#Macros" title="Macros">
9<link rel="prev" href="Macro-Arguments.html#Macro-Arguments" title="Macro Arguments">
10<link rel="next" href="Concatenation.html#Concatenation" title="Concatenation">
11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
12<!--
13Copyright (C) 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996,
141997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
152008, 2009, 2010, 2011
16Free Software Foundation, Inc.
17
18Permission is granted to copy, distribute and/or modify this document
19under the terms of the GNU Free Documentation License, Version 1.3 or
20any later version published by the Free Software Foundation.  A copy of
21the license is included in the
22section entitled ``GNU Free Documentation License''.
23
24This manual contains no Invariant Sections.  The Front-Cover Texts are
25(a) (see below), and the Back-Cover Texts are (b) (see below).
26
27(a) The FSF's Front-Cover Text is:
28
29     A GNU Manual
30
31(b) The FSF's Back-Cover Text is:
32
33     You have freedom to copy and modify this GNU Manual, like GNU
34     software.  Copies published by the Free Software Foundation raise
35     funds for GNU development.
36-->
37<meta http-equiv="Content-Style-Type" content="text/css">
38<style type="text/css"><!--
39  pre.display { font-family:inherit }
40  pre.format  { font-family:inherit }
41  pre.smalldisplay { font-family:inherit; font-size:smaller }
42  pre.smallformat  { font-family:inherit; font-size:smaller }
43  pre.smallexample { font-size:smaller }
44  pre.smalllisp    { font-size:smaller }
45  span.sc    { font-variant:small-caps }
46  span.roman { font-family:serif; font-weight:normal; } 
47  span.sansserif { font-family:sans-serif; font-weight:normal; } 
48--></style>
49<link rel="stylesheet" type="text/css" href="../cs.css">
50</head>
51<body>
52<div class="node">
53<a name="Stringification"></a>
54<p>
55Next:&nbsp;<a rel="next" accesskey="n" href="Concatenation.html#Concatenation">Concatenation</a>,
56Previous:&nbsp;<a rel="previous" accesskey="p" href="Macro-Arguments.html#Macro-Arguments">Macro Arguments</a>,
57Up:&nbsp;<a rel="up" accesskey="u" href="Macros.html#Macros">Macros</a>
58<hr>
59</div>
60
61<h3 class="section">3.4 Stringification</h3>
62
63<p><a name="index-stringification-50"></a><a name="index-g_t_0040samp_007b_0023_007d-operator-51"></a>
64Sometimes you may want to convert a macro argument into a string
65constant.  Parameters are not replaced inside string constants, but you
66can use the &lsquo;<samp><span class="samp">#</span></samp>&rsquo; preprocessing operator instead.  When a macro
67parameter is used with a leading &lsquo;<samp><span class="samp">#</span></samp>&rsquo;, the preprocessor replaces it
68with the literal text of the actual argument, converted to a string
69constant.  Unlike normal parameter replacement, the argument is not
70macro-expanded first.  This is called <dfn>stringification</dfn>.
71
72   <p>There is no way to combine an argument with surrounding text and
73stringify it all together.  Instead, you can write a series of adjacent
74string constants and stringified arguments.  The preprocessor will
75replace the stringified arguments with string constants.  The C
76compiler will then combine all the adjacent string constants into one
77long string.
78
79   <p>Here is an example of a macro definition that uses stringification:
80
81<pre class="smallexample">     #define WARN_IF(EXP) \
82     do { if (EXP) \
83             fprintf (stderr, "Warning: " #EXP "\n"); } \
84     while (0)
85     WARN_IF (x == 0);
86          ==&gt; do { if (x == 0)
87                fprintf (stderr, "Warning: " "x == 0" "\n"); } while (0);
88</pre>
89   <p class="noindent">The argument for <code>EXP</code> is substituted once, as-is, into the
90<code>if</code> statement, and once, stringified, into the argument to
91<code>fprintf</code>.  If <code>x</code> were a macro, it would be expanded in the
92<code>if</code> statement, but not in the string.
93
94   <p>The <code>do</code> and <code>while (0)</code> are a kludge to make it possible to
95write <code>WARN_IF (</code><var>arg</var><code>);</code>, which the resemblance of
96<code>WARN_IF</code> to a function would make C programmers want to do; see
97<a href="Swallowing-the-Semicolon.html#Swallowing-the-Semicolon">Swallowing the Semicolon</a>.
98
99   <p>Stringification in C involves more than putting double-quote characters
100around the fragment.  The preprocessor backslash-escapes the quotes
101surrounding embedded string constants, and all backslashes within string and
102character constants, in order to get a valid C string constant with the
103proper contents.  Thus, stringifying <code>p&nbsp;=&nbsp;"foo\n";<!-- /@w --></code> results in
104<tt>"p&nbsp;=&nbsp;\"foo\\n\";"<!-- /@w --></tt>.  However, backslashes that are not inside string
105or character constants are not duplicated: &lsquo;<samp><span class="samp">\n</span></samp>&rsquo; by itself
106stringifies to <tt>"\n"</tt>.
107
108   <p>All leading and trailing whitespace in text being stringified is
109ignored.  Any sequence of whitespace in the middle of the text is
110converted to a single space in the stringified result.  Comments are
111replaced by whitespace long before stringification happens, so they
112never appear in stringified text.
113
114   <p>There is no way to convert a macro argument into a character constant.
115
116   <p>If you want to stringify the result of expansion of a macro argument,
117you have to use two levels of macros.
118
119<pre class="smallexample">     #define xstr(s) str(s)
120     #define str(s) #s
121     #define foo 4
122     str (foo)
123          ==&gt; "foo"
124     xstr (foo)
125          ==&gt; xstr (4)
126          ==&gt; str (4)
127          ==&gt; "4"
128</pre>
129   <p><code>s</code> is stringified when it is used in <code>str</code>, so it is not
130macro-expanded first.  But <code>s</code> is an ordinary argument to
131<code>xstr</code>, so it is completely macro-expanded before <code>xstr</code>
132itself is expanded (see <a href="Argument-Prescan.html#Argument-Prescan">Argument Prescan</a>).  Therefore, by the time
133<code>str</code> gets to its argument, it has already been macro-expanded.
134
135   </body></html>
136
137