source: public/doc/gnu-c/Stringification.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 7.0 KB
Line 
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
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing 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<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.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>
60Next: <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> &nbsp; [<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
68constant. Parameters are not replaced inside string constants, but
69you can use the <code>#</code> preprocessing operator instead. When a macro
70parameter is used with a leading <code>#</code>, preprocessing replaces it
71with the literal text of the actual argument, converted to a string
72constant. Unlike normal parameter replacement, the argument is not
73macro-expanded first. This is called <em>stringification</em>.
74</p>
75<p>There is no way to combine an argument with surrounding text and
76stringify it all together. But you can write a series of string
77constants and stringified arguments. After preprocessing replaces the
78stringified arguments with string constants, the consecutive string
79constants 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
83string constants:
84</p>
85<div class="example">
86<pre class="example">#define WARN_IF(EXP) \
87 do { if (EXP) \
88 fprintf (stderr, &quot;Warning: &quot; #EXP &quot;\n&quot;); } \
89 while (0)
90
91WARN_IF (x == 0);
92 &rarr;
93 do { if (x == 0)
94 fprintf (stderr, &quot;Warning: &quot; &quot;x == 0&quot; &quot;\n&quot;); }
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
104write <code>WARN_IF (<var>arg</var>);</code>. The resemblance of <code>WARN_IF</code>
105to a function makes that a natural way to write it.
106See <a href="Swallowing-the-Semicolon.html">Swallowing the Semicolon</a>.
107</p>
108<p>Stringification in C involves more than putting double-quote
109characters around the fragment. It also backslash-escapes the quotes
110surrounding embedded string constants, and all backslashes within
111string and character constants, in order to get a valid C string
112constant with the proper contents. Thus, stringifying <code>p&nbsp;=&nbsp;&quot;foo\n&quot;;<!-- /@w --></code> results in <tt>&quot;p&nbsp;=&nbsp;\&quot;foo\\n\&quot;;&quot;<!-- /@w --></tt>. However, backslashes
113that are not inside string or character constants are not duplicated:
114&lsquo;<samp>\n</samp>&rsquo; by itself stringifies to <tt>&quot;\n&quot;</tt>.
115</p>
116<p>All leading and trailing whitespace in text being stringified is
117ignored. Any sequence of whitespace in the middle of the text is
118converted to a single space in the stringified result. Comments are
119replaced by whitespace long before stringification happens, so they
120never 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
125use 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
131str (foo)
132 &rarr; &quot;foo&quot;
133xstr (foo)
134 &rarr; xstr (4)
135 &rarr; str (4)
136 &rarr; &quot;4&quot;
137</pre></div>
138
139<p><code>s</code> is stringified when it is used in <code>str</code>, so it is not
140macro-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>
142itself 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
144macro-expanded.
145</p>
146<hr>
147<div class="header">
148<p>
149Next: <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> &nbsp; [<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>
Note: See TracBrowser for help on using the repository browser.