Object_002dlike-Macros.html 8.3 KB

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