Stringizing.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1987-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation. A copy of
  7. the license is included in the
  8. section entitled "GNU Free Documentation License".
  9. This manual contains no Invariant Sections. The Front-Cover Texts are
  10. (a) (see below), and the Back-Cover Texts are (b) (see below).
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>The C Preprocessor: Stringizing</title>
  20. <meta name="description" content="The C Preprocessor: Stringizing">
  21. <meta name="keywords" content="The C Preprocessor: Stringizing">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Index-of-Directives.html#Index-of-Directives" rel="index" title="Index of Directives">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Macros.html#Macros" rel="up" title="Macros">
  30. <link href="Concatenation.html#Concatenation" rel="next" title="Concatenation">
  31. <link href="Macro-Arguments.html#Macro-Arguments" rel="prev" title="Macro Arguments">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Stringizing"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Concatenation.html#Concatenation" accesskey="n" rel="next">Concatenation</a>, Previous: <a href="Macro-Arguments.html#Macro-Arguments" accesskey="p" rel="prev">Macro Arguments</a>, Up: <a href="Macros.html#Macros" accesskey="u" rel="up">Macros</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index-of-Directives.html#Index-of-Directives" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="Stringizing-1"></a>
  69. <h3 class="section">3.4 Stringizing</h3>
  70. <a name="index-stringizing"></a>
  71. <a name="index-_0023-operator"></a>
  72. <p>Sometimes you may want to convert a macro argument into a string
  73. constant. Parameters are not replaced inside string constants, but you
  74. can use the &lsquo;<samp>#</samp>&rsquo; preprocessing operator instead. When a macro
  75. parameter is used with a leading &lsquo;<samp>#</samp>&rsquo;, the preprocessor replaces it
  76. with the literal text of the actual argument, converted to a string
  77. constant. Unlike normal parameter replacement, the argument is not
  78. macro-expanded first. This is called <em>stringizing</em>.
  79. </p>
  80. <p>There is no way to combine an argument with surrounding text and
  81. stringize it all together. Instead, you can write a series of adjacent
  82. string constants and stringized arguments. The preprocessor
  83. replaces the stringized arguments with string constants. The C
  84. compiler then combines all the adjacent string constants into one
  85. long string.
  86. </p>
  87. <p>Here is an example of a macro definition that uses stringizing:
  88. </p>
  89. <div class="smallexample">
  90. <pre class="smallexample">#define WARN_IF(EXP) \
  91. do { if (EXP) \
  92. fprintf (stderr, &quot;Warning: &quot; #EXP &quot;\n&quot;); } \
  93. while (0)
  94. WARN_IF (x == 0);
  95. &rarr; do { if (x == 0)
  96. fprintf (stderr, &quot;Warning: &quot; &quot;x == 0&quot; &quot;\n&quot;); } while (0);
  97. </pre></div>
  98. <p>The argument for <code>EXP</code> is substituted once, as-is, into the
  99. <code>if</code> statement, and once, stringized, 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>, which the resemblance of
  105. <code>WARN_IF</code> to a function would make C programmers want to do; see
  106. <a href="Swallowing-the-Semicolon.html#Swallowing-the-Semicolon">Swallowing the Semicolon</a>.
  107. </p>
  108. <p>Stringizing in C involves more than putting double-quote characters
  109. around the fragment. The preprocessor backslash-escapes the quotes
  110. surrounding embedded string constants, and all backslashes within string and
  111. character constants, in order to get a valid C string constant with the
  112. proper contents. Thus, stringizing <code>p&nbsp;=&nbsp;&quot;foo\n&quot;;<!-- /@w --></code> results in
  113. <tt>&quot;p&nbsp;=&nbsp;\&quot;foo\\n\&quot;;&quot;<!-- /@w --></tt>. However, backslashes that are not inside string
  114. or character constants are not duplicated: &lsquo;<samp>\n</samp>&rsquo; by itself
  115. stringizes to <tt>&quot;\n&quot;</tt>.
  116. </p>
  117. <p>All leading and trailing whitespace in text being stringized is
  118. ignored. Any sequence of whitespace in the middle of the text is
  119. converted to a single space in the stringized result. Comments are
  120. replaced by whitespace long before stringizing happens, so they
  121. never appear in stringized text.
  122. </p>
  123. <p>There is no way to convert a macro argument into a character constant.
  124. </p>
  125. <p>If you want to stringize the result of expansion of a macro argument,
  126. you have to use two levels of macros.
  127. </p>
  128. <div class="smallexample">
  129. <pre class="smallexample">#define xstr(s) str(s)
  130. #define str(s) #s
  131. #define foo 4
  132. str (foo)
  133. &rarr; &quot;foo&quot;
  134. xstr (foo)
  135. &rarr; xstr (4)
  136. &rarr; str (4)
  137. &rarr; &quot;4&quot;
  138. </pre></div>
  139. <p><code>s</code> is stringized 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">Argument Prescan</a>). Therefore, by the time
  143. <code>str</code> gets to its argument, it has already been macro-expanded.
  144. </p>
  145. <hr>
  146. <div class="header">
  147. <p>
  148. Next: <a href="Concatenation.html#Concatenation" accesskey="n" rel="next">Concatenation</a>, Previous: <a href="Macro-Arguments.html#Macro-Arguments" accesskey="p" rel="prev">Macro Arguments</a>, Up: <a href="Macros.html#Macros" accesskey="u" rel="up">Macros</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index-of-Directives.html#Index-of-Directives" title="Index" rel="index">Index</a>]</p>
  149. </div>
  150. </body>
  151. </html>