Object_002dlike-Macros.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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>Object-like Macros (The C Preprocessor)</title>
  21. <meta name="description" content="Object-like Macros (The C Preprocessor)">
  22. <meta name="keywords" content="Object-like Macros (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="Function_002dlike-Macros.html" rel="next" title="Function-like Macros">
  31. <link href="Macros.html" rel="prev" title="Macros">
  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="Object_002dlike-Macros"></span><div class="header">
  53. <p>
  54. Next: <a href="Function_002dlike-Macros.html" accesskey="n" rel="next">Function-like Macros</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="Object_002dlike-Macros-1"></span><h3 class="section">3.1 Object-like Macros</h3>
  58. <span id="index-object_002dlike-macro"></span>
  59. <span id="index-symbolic-constants"></span>
  60. <span id="index-manifest-constants"></span>
  61. <p>An <em>object-like macro</em> is a simple identifier which will be replaced
  62. by a code fragment. It is called object-like because it looks like a
  63. data object in code that uses it. They are most commonly used to give
  64. symbolic names to numeric constants.
  65. </p>
  66. <span id="index-_0023define"></span>
  67. <p>You create macros with the &lsquo;<samp>#define</samp>&rsquo; directive. &lsquo;<samp>#define</samp>&rsquo; is
  68. followed by the name of the macro and then the token sequence it should
  69. be an abbreviation for, which is variously referred to as the macro&rsquo;s
  70. <em>body</em>, <em>expansion</em> or <em>replacement list</em>. For example,
  71. </p>
  72. <div class="example">
  73. <pre class="example">#define BUFFER_SIZE 1024
  74. </pre></div>
  75. <p>defines a macro named <code>BUFFER_SIZE</code> as an abbreviation for the
  76. token <code>1024</code>. If somewhere after this &lsquo;<samp>#define</samp>&rsquo; directive
  77. there comes a C statement of the form
  78. </p>
  79. <div class="example">
  80. <pre class="example">foo = (char *) malloc (BUFFER_SIZE);
  81. </pre></div>
  82. <p>then the C preprocessor will recognize and <em>expand</em> the macro
  83. <code>BUFFER_SIZE</code>. The C compiler will see the same tokens as it would
  84. if you had written
  85. </p>
  86. <div class="example">
  87. <pre class="example">foo = (char *) malloc (1024);
  88. </pre></div>
  89. <p>By convention, macro names are written in uppercase. Programs are
  90. easier to read when it is possible to tell at a glance which names are
  91. macros.
  92. </p>
  93. <p>The macro&rsquo;s body ends at the end of the &lsquo;<samp>#define</samp>&rsquo; line. You may
  94. continue the definition onto multiple lines, if necessary, using
  95. backslash-newline. When the macro is expanded, however, it will all
  96. come out on one line. For example,
  97. </p>
  98. <div class="example">
  99. <pre class="example">#define NUMBERS 1, \
  100. 2, \
  101. 3
  102. int x[] = { NUMBERS };
  103. &rarr; int x[] = { 1, 2, 3 };
  104. </pre></div>
  105. <p>The most common visible consequence of this is surprising line numbers
  106. in error messages.
  107. </p>
  108. <p>There is no restriction on what can go in a macro body provided it
  109. decomposes into valid preprocessing tokens. Parentheses need not
  110. balance, and the body need not resemble valid C code. (If it does not,
  111. you may get error messages from the C compiler when you use the macro.)
  112. </p>
  113. <p>The C preprocessor scans your program sequentially. Macro definitions
  114. take effect at the place you write them. Therefore, the following input
  115. to the C preprocessor
  116. </p>
  117. <div class="example">
  118. <pre class="example">foo = X;
  119. #define X 4
  120. bar = X;
  121. </pre></div>
  122. <p>produces
  123. </p>
  124. <div class="example">
  125. <pre class="example">foo = X;
  126. bar = 4;
  127. </pre></div>
  128. <p>When the preprocessor expands a macro name, the macro&rsquo;s expansion
  129. replaces the macro invocation, then the expansion is examined for more
  130. macros to expand. For example,
  131. </p>
  132. <div class="example">
  133. <pre class="example">#define TABLESIZE BUFSIZE
  134. #define BUFSIZE 1024
  135. TABLESIZE
  136. &rarr; BUFSIZE
  137. &rarr; 1024
  138. </pre></div>
  139. <p><code>TABLESIZE</code> is expanded first to produce <code>BUFSIZE</code>, then that
  140. macro is expanded to produce the final result, <code>1024</code>.
  141. </p>
  142. <p>Notice that <code>BUFSIZE</code> was not defined when <code>TABLESIZE</code> was
  143. defined. The &lsquo;<samp>#define</samp>&rsquo; for <code>TABLESIZE</code> uses exactly the
  144. expansion you specify&mdash;in this case, <code>BUFSIZE</code>&mdash;and does not
  145. check to see whether it too contains macro names. Only when you
  146. <em>use</em> <code>TABLESIZE</code> is the result of its expansion scanned for
  147. more macro names.
  148. </p>
  149. <p>This makes a difference if you change the definition of <code>BUFSIZE</code>
  150. at some point in the source file. <code>TABLESIZE</code>, defined as shown,
  151. will always expand using the definition of <code>BUFSIZE</code> that is
  152. currently in effect:
  153. </p>
  154. <div class="example">
  155. <pre class="example">#define BUFSIZE 1020
  156. #define TABLESIZE BUFSIZE
  157. #undef BUFSIZE
  158. #define BUFSIZE 37
  159. </pre></div>
  160. <p>Now <code>TABLESIZE</code> expands (in two stages) to <code>37</code>.
  161. </p>
  162. <p>If the expansion of a macro contains its own name, either directly or
  163. via intermediate macros, it is not expanded again when the expansion is
  164. examined for more macros. This prevents infinite recursion.
  165. See <a href="Self_002dReferential-Macros.html">Self-Referential Macros</a>, for the precise details.
  166. </p>
  167. <hr>
  168. <div class="header">
  169. <p>
  170. Next: <a href="Function_002dlike-Macros.html" accesskey="n" rel="next">Function-like Macros</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>
  171. </div>
  172. </body>
  173. </html>