Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

před 3 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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-2020 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.5, 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#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.indentedblock {margin-right: 0em}
  36. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  37. blockquote.smallquotation {font-size: smaller}
  38. div.display {margin-left: 3.2em}
  39. div.example {margin-left: 3.2em}
  40. div.lisp {margin-left: 3.2em}
  41. div.smalldisplay {margin-left: 3.2em}
  42. div.smallexample {margin-left: 3.2em}
  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.nolinebreak {white-space: nowrap}
  54. span.roman {font-family: initial; font-weight: normal}
  55. span.sansserif {font-family: sans-serif; font-weight: normal}
  56. ul.no-bullet {list-style: none}
  57. -->
  58. </style>
  59. </head>
  60. <body lang="en">
  61. <a name="Stringizing"></a>
  62. <div class="header">
  63. <p>
  64. 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>
  65. </div>
  66. <hr>
  67. <a name="Stringizing-1"></a>
  68. <h3 class="section">3.4 Stringizing</h3>
  69. <a name="index-stringizing"></a>
  70. <a name="index-_0023-operator"></a>
  71. <p>Sometimes you may want to convert a macro argument into a string
  72. constant. Parameters are not replaced inside string constants, but you
  73. can use the &lsquo;<samp>#</samp>&rsquo; preprocessing operator instead. When a macro
  74. parameter is used with a leading &lsquo;<samp>#</samp>&rsquo;, the preprocessor replaces it
  75. with the literal text of the actual argument, converted to a string
  76. constant. Unlike normal parameter replacement, the argument is not
  77. macro-expanded first. This is called <em>stringizing</em>.
  78. </p>
  79. <p>There is no way to combine an argument with surrounding text and
  80. stringize it all together. Instead, you can write a series of adjacent
  81. string constants and stringized arguments. The preprocessor
  82. replaces the stringized arguments with string constants. The C
  83. compiler then combines all the adjacent string constants into one
  84. long string.
  85. </p>
  86. <p>Here is an example of a macro definition that uses stringizing:
  87. </p>
  88. <div class="smallexample">
  89. <pre class="smallexample">#define WARN_IF(EXP) \
  90. do { if (EXP) \
  91. fprintf (stderr, &quot;Warning: &quot; #EXP &quot;\n&quot;); } \
  92. while (0)
  93. WARN_IF (x == 0);
  94. &rarr; do { if (x == 0)
  95. fprintf (stderr, &quot;Warning: &quot; &quot;x == 0&quot; &quot;\n&quot;); } while (0);
  96. </pre></div>
  97. <p>The argument for <code>EXP</code> is substituted once, as-is, into the
  98. <code>if</code> statement, and once, stringized, into the argument to
  99. <code>fprintf</code>. If <code>x</code> were a macro, it would be expanded in the
  100. <code>if</code> statement, but not in the string.
  101. </p>
  102. <p>The <code>do</code> and <code>while (0)</code> are a kludge to make it possible to
  103. write <code>WARN_IF (<var>arg</var>);</code>, which the resemblance of
  104. <code>WARN_IF</code> to a function would make C programmers want to do; see
  105. <a href="Swallowing-the-Semicolon.html#Swallowing-the-Semicolon">Swallowing the Semicolon</a>.
  106. </p>
  107. <p>Stringizing in C involves more than putting double-quote characters
  108. around the fragment. The preprocessor backslash-escapes the quotes
  109. surrounding embedded string constants, and all backslashes within string and
  110. character constants, in order to get a valid C string constant with the
  111. proper contents. Thus, stringizing <code>p&nbsp;=&nbsp;&quot;foo\n&quot;;<!-- /@w --></code> results in
  112. <tt>&quot;p&nbsp;=&nbsp;\&quot;foo\\n\&quot;;&quot;<!-- /@w --></tt>. However, backslashes that are not inside string
  113. or character constants are not duplicated: &lsquo;<samp>\n</samp>&rsquo; by itself
  114. stringizes to <tt>&quot;\n&quot;</tt>.
  115. </p>
  116. <p>All leading and trailing whitespace in text being stringized is
  117. ignored. Any sequence of whitespace in the middle of the text is
  118. converted to a single space in the stringized result. Comments are
  119. replaced by whitespace long before stringizing happens, so they
  120. never appear in stringized text.
  121. </p>
  122. <p>There is no way to convert a macro argument into a character constant.
  123. </p>
  124. <p>If you want to stringize the result of expansion of a macro argument,
  125. you have to use two levels of macros.
  126. </p>
  127. <div class="smallexample">
  128. <pre class="smallexample">#define xstr(s) str(s)
  129. #define str(s) #s
  130. #define foo 4
  131. str (foo)
  132. &rarr; &quot;foo&quot;
  133. xstr (foo)
  134. &rarr; xstr (4)
  135. &rarr; str (4)
  136. &rarr; &quot;4&quot;
  137. </pre></div>
  138. <p><code>s</code> is stringized when it is used in <code>str</code>, so it is not
  139. macro-expanded first. But <code>s</code> is an ordinary argument to
  140. <code>xstr</code>, so it is completely macro-expanded before <code>xstr</code>
  141. itself is expanded (see <a href="Argument-Prescan.html#Argument-Prescan">Argument Prescan</a>). Therefore, by the time
  142. <code>str</code> gets to its argument, it has already been macro-expanded.
  143. </p>
  144. <hr>
  145. <div class="header">
  146. <p>
  147. 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>
  148. </div>
  149. </body>
  150. </html>