1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Stringification (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Stringification (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Stringification (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Macros.html" rel="up" title="Macros">
|
---|
33 | <link href="Concatenation.html" rel="next" title="Concatenation">
|
---|
34 | <link href="Macro-Arguments.html" rel="prev" title="Macro Arguments">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Stringification"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Concatenation.html" accesskey="n" rel="next">Concatenation</a>, Previous: <a href="Macro-Arguments.html" accesskey="p" rel="prev">Macro Arguments</a>, Up: <a href="Macros.html" accesskey="u" rel="up">Macros</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Stringification-1"></span><h4 class="subsection">26.5.4 Stringification</h4>
|
---|
64 | <span id="index-stringification"></span>
|
---|
65 | <span id="index-_0023-operator"></span>
|
---|
66 |
|
---|
67 | <p>Sometimes you may want to convert a macro argument into a string
|
---|
68 | constant. Parameters are not replaced inside string constants, but
|
---|
69 | you can use the <code>#</code> preprocessing operator instead. When a macro
|
---|
70 | parameter is used with a leading <code>#</code>, preprocessing replaces it
|
---|
71 | with the literal text of the actual argument, converted to a string
|
---|
72 | constant. Unlike normal parameter replacement, the argument is not
|
---|
73 | macro-expanded first. This is called <em>stringification</em>.
|
---|
74 | </p>
|
---|
75 | <p>There is no way to combine an argument with surrounding text and
|
---|
76 | stringify it all together. But you can write a series of string
|
---|
77 | constants and stringified arguments. After preprocessing replaces the
|
---|
78 | stringified arguments with string constants, the consecutive string
|
---|
79 | constants will be concatenated into one long string constant
|
---|
80 | (see <a href="String-Constants.html">String Constants</a>).
|
---|
81 | </p>
|
---|
82 | <p>Here is an example that uses stringification and concatenation of
|
---|
83 | string constants:
|
---|
84 | </p>
|
---|
85 | <div class="example">
|
---|
86 | <pre class="example">#define WARN_IF(EXP) \
|
---|
87 | do { if (EXP) \
|
---|
88 | fprintf (stderr, "Warning: " #EXP "\n"); } \
|
---|
89 | while (0)
|
---|
90 |
|
---|
91 | WARN_IF (x == 0);
|
---|
92 | →
|
---|
93 | do { if (x == 0)
|
---|
94 | fprintf (stderr, "Warning: " "x == 0" "\n"); }
|
---|
95 | while (0);
|
---|
96 | </pre></div>
|
---|
97 |
|
---|
98 | <p>The argument for <code>EXP</code> is substituted once, as is, into the
|
---|
99 | <code>if</code> statement, and once, stringified, into the argument to
|
---|
100 | <code>fprintf</code>. If <code>x</code> were a macro, it would be expanded in the
|
---|
101 | <code>if</code> statement but not in the string.
|
---|
102 | </p>
|
---|
103 | <p>The <code>do</code> and <code>while (0)</code> are a kludge to make it possible to
|
---|
104 | write <code>WARN_IF (<var>arg</var>);</code>. The resemblance of <code>WARN_IF</code>
|
---|
105 | to a function makes that a natural way to write it.
|
---|
106 | See <a href="Swallowing-the-Semicolon.html">Swallowing the Semicolon</a>.
|
---|
107 | </p>
|
---|
108 | <p>Stringification in C involves more than putting double-quote
|
---|
109 | characters around the fragment. It also backslash-escapes the quotes
|
---|
110 | surrounding embedded string constants, and all backslashes within
|
---|
111 | string and character constants, in order to get a valid C string
|
---|
112 | constant with the proper contents. Thus, stringifying <code>p = "foo\n";<!-- /@w --></code> results in <tt>"p = \"foo\\n\";"<!-- /@w --></tt>. However, backslashes
|
---|
113 | that are not inside string or character constants are not duplicated:
|
---|
114 | ‘<samp>\n</samp>’ by itself stringifies to <tt>"\n"</tt>.
|
---|
115 | </p>
|
---|
116 | <p>All leading and trailing whitespace in text being stringified is
|
---|
117 | ignored. Any sequence of whitespace in the middle of the text is
|
---|
118 | converted to a single space in the stringified result. Comments are
|
---|
119 | replaced by whitespace long before stringification happens, so they
|
---|
120 | never appear in stringified text.
|
---|
121 | </p>
|
---|
122 | <p>There is no way to convert a macro argument into a character constant.
|
---|
123 | </p>
|
---|
124 | <p>To stringify the result of expansion of a macro argument, you have to
|
---|
125 | use two levels of macros, like this:
|
---|
126 | </p>
|
---|
127 | <div class="example">
|
---|
128 | <pre class="example">#define xstr(S) str(S)
|
---|
129 | #define str(s) #s
|
---|
130 | #define foo 4
|
---|
131 | str (foo)
|
---|
132 | → "foo"
|
---|
133 | xstr (foo)
|
---|
134 | → xstr (4)
|
---|
135 | → str (4)
|
---|
136 | → "4"
|
---|
137 | </pre></div>
|
---|
138 |
|
---|
139 | <p><code>s</code> is stringified when it is used in <code>str</code>, so it is not
|
---|
140 | macro-expanded first. But <code>S</code> is an ordinary argument to
|
---|
141 | <code>xstr</code>, so it is completely macro-expanded before <code>xstr</code>
|
---|
142 | itself is expanded (see <a href="Argument-Prescan.html">Argument Prescan</a>). Therefore, by the time
|
---|
143 | <code>str</code> gets to its argument text, that text already been
|
---|
144 | macro-expanded.
|
---|
145 | </p>
|
---|
146 | <hr>
|
---|
147 | <div class="header">
|
---|
148 | <p>
|
---|
149 | Next: <a href="Concatenation.html" accesskey="n" rel="next">Concatenation</a>, Previous: <a href="Macro-Arguments.html" accesskey="p" rel="prev">Macro Arguments</a>, Up: <a href="Macros.html" accesskey="u" rel="up">Macros</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
150 | </div>
|
---|
151 |
|
---|
152 |
|
---|
153 |
|
---|
154 | </body>
|
---|
155 | </html>
|
---|