You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
8.2KB

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