Stringizing.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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-2023 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 6.7, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>Stringizing (The C Preprocessor)</title>
  21. <meta name="description" content="Stringizing (The C Preprocessor)">
  22. <meta name="keywords" content="Stringizing (The C Preprocessor)">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  26. <link href="index.html" rel="start" title="Top">
  27. <link href="Index-of-Directives.html" rel="index" title="Index of Directives">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Macros.html" rel="up" title="Macros">
  30. <link href="Concatenation.html" rel="next" title="Concatenation">
  31. <link href="Macro-Arguments.html" rel="prev" title="Macro Arguments">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.indentedblock {margin-right: 0em}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.lisp {margin-left: 3.2em}
  39. kbd {font-style: oblique}
  40. pre.display {font-family: inherit}
  41. pre.format {font-family: inherit}
  42. pre.menu-comment {font-family: serif}
  43. pre.menu-preformatted {font-family: serif}
  44. span.nolinebreak {white-space: nowrap}
  45. span.roman {font-family: initial; font-weight: normal}
  46. span.sansserif {font-family: sans-serif; font-weight: normal}
  47. ul.no-bullet {list-style: none}
  48. -->
  49. </style>
  50. </head>
  51. <body lang="en">
  52. <span id="Stringizing"></span><div class="header">
  53. <p>
  54. 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> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index-of-Directives.html" title="Index" rel="index">Index</a>]</p>
  55. </div>
  56. <hr>
  57. <span id="Stringizing-1"></span><h3 class="section">3.4 Stringizing</h3>
  58. <span id="index-stringizing"></span>
  59. <span id="index-_0023-operator"></span>
  60. <p>Sometimes you may want to convert a macro argument into a string
  61. constant. Parameters are not replaced inside string constants, but you
  62. can use the &lsquo;<samp>#</samp>&rsquo; preprocessing operator instead. When a macro
  63. parameter is used with a leading &lsquo;<samp>#</samp>&rsquo;, the preprocessor replaces it
  64. with the literal text of the actual argument, converted to a string
  65. constant. Unlike normal parameter replacement, the argument is not
  66. macro-expanded first. This is called <em>stringizing</em>.
  67. </p>
  68. <p>There is no way to combine an argument with surrounding text and
  69. stringize it all together. Instead, you can write a series of adjacent
  70. string constants and stringized arguments. The preprocessor
  71. replaces the stringized arguments with string constants. The C
  72. compiler then combines all the adjacent string constants into one
  73. long string.
  74. </p>
  75. <p>Here is an example of a macro definition that uses stringizing:
  76. </p>
  77. <div class="example">
  78. <pre class="example">#define WARN_IF(EXP) \
  79. do { if (EXP) \
  80. fprintf (stderr, &quot;Warning: &quot; #EXP &quot;\n&quot;); } \
  81. while (0)
  82. WARN_IF (x == 0);
  83. &rarr; do { if (x == 0)
  84. fprintf (stderr, &quot;Warning: &quot; &quot;x == 0&quot; &quot;\n&quot;); } while (0);
  85. </pre></div>
  86. <p>The argument for <code>EXP</code> is substituted once, as-is, into the
  87. <code>if</code> statement, and once, stringized, into the argument to
  88. <code>fprintf</code>. If <code>x</code> were a macro, it would be expanded in the
  89. <code>if</code> statement, but not in the string.
  90. </p>
  91. <p>The <code>do</code> and <code>while (0)</code> are a kludge to make it possible to
  92. write <code>WARN_IF (<var>arg</var>);</code>, which the resemblance of
  93. <code>WARN_IF</code> to a function would make C programmers want to do; see
  94. <a href="Swallowing-the-Semicolon.html">Swallowing the Semicolon</a>.
  95. </p>
  96. <p>Stringizing in C involves more than putting double-quote characters
  97. around the fragment. The preprocessor backslash-escapes the quotes
  98. surrounding embedded string constants, and all backslashes within string and
  99. character constants, in order to get a valid C string constant with the
  100. proper contents. Thus, stringizing <code>p&nbsp;=&nbsp;&quot;foo\n&quot;;<!-- /@w --></code> results in
  101. <tt>&quot;p&nbsp;=&nbsp;\&quot;foo\\n\&quot;;&quot;<!-- /@w --></tt>. However, backslashes that are not inside string
  102. or character constants are not duplicated: &lsquo;<samp>\n</samp>&rsquo; by itself
  103. stringizes to <tt>&quot;\n&quot;</tt>.
  104. </p>
  105. <p>All leading and trailing whitespace in text being stringized is
  106. ignored. Any sequence of whitespace in the middle of the text is
  107. converted to a single space in the stringized result. Comments are
  108. replaced by whitespace long before stringizing happens, so they
  109. never appear in stringized text.
  110. </p>
  111. <p>There is no way to convert a macro argument into a character constant.
  112. </p>
  113. <p>If you want to stringize the result of expansion of a macro argument,
  114. you have to use two levels of macros.
  115. </p>
  116. <div class="example">
  117. <pre class="example">#define xstr(s) str(s)
  118. #define str(s) #s
  119. #define foo 4
  120. str (foo)
  121. &rarr; &quot;foo&quot;
  122. xstr (foo)
  123. &rarr; xstr (4)
  124. &rarr; str (4)
  125. &rarr; &quot;4&quot;
  126. </pre></div>
  127. <p><code>s</code> is stringized when it is used in <code>str</code>, so it is not
  128. macro-expanded first. But <code>s</code> is an ordinary argument to
  129. <code>xstr</code>, so it is completely macro-expanded before <code>xstr</code>
  130. itself is expanded (see <a href="Argument-Prescan.html">Argument Prescan</a>). Therefore, by the time
  131. <code>str</code> gets to its argument, it has already been macro-expanded.
  132. </p>
  133. <hr>
  134. <div class="header">
  135. <p>
  136. 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> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index-of-Directives.html" title="Index" rel="index">Index</a>]</p>
  137. </div>
  138. </body>
  139. </html>