⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 object-like-macros.html

📁 gcc手册
💻 HTML
字号:
<html lang="en">

<head>

<title>The C Preprocessor</title>

<meta http-equiv="Content-Type" content="text/html">

<meta name="description" content="The C Preprocessor">

<meta name="generator" content="makeinfo 4.3">

<link href="http://www.gnu.org/software/texinfo/" rel="generator-home">

<!--

Copyright &copy; 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996,

1997, 1998, 1999, 2000, 2001, 2002, 2003

Free Software Foundation, Inc.



   <p>Permission is granted to copy, distribute and/or modify this document

under the terms of the GNU Free Documentation License, Version 1.1 or

any later version published by the Free Software Foundation.  A copy of

the license is included in the

section entitled "GNU Free Documentation License".



   <p>This manual contains no Invariant Sections.  The Front-Cover Texts are

(a) (see below), and the Back-Cover Texts are (b) (see below).



   <p>(a) The FSF's Front-Cover Text is:



   <p>A GNU Manual



   <p>(b) The FSF's Back-Cover Text is:



   <p>You have freedom to copy and modify this GNU Manual, like GNU

     software.  Copies published by the Free Software Foundation raise

     funds for GNU development. 

-->

</head>

<body>

<div class="node">

<p>

Node:<a name="Object-like%20Macros">Object-like Macros</a>,

Next:<a rel="next" accesskey="n" href="Function-like-Macros.html#Function-like%20Macros">Function-like Macros</a>,

Up:<a rel="up" accesskey="u" href="Macros.html#Macros">Macros</a>

<hr><br>

</div>



<h3 class="section">Object-like Macros</h3>



   <p>An <dfn>object-like macro</dfn> is a simple identifier which will be replaced

by a code fragment.  It is called object-like because it looks like a

data object in code that uses it.  They are most commonly used to give

symbolic names to numeric constants.



   <p>You create macros with the <code>#define</code> directive.  <code>#define</code> is

followed by the name of the macro and then the token sequence it should

be an abbreviation for, which is variously referred to as the macro's

<dfn>body</dfn>, <dfn>expansion</dfn> or <dfn>replacement list</dfn>.  For example,



<pre class="example">     #define BUFFER_SIZE 1024

     </pre>



<p>defines a macro named <code>BUFFER_SIZE</code> as an abbreviation for the

token <code>1024</code>.  If somewhere after this <code>#define</code> directive

there comes a C statement of the form



<pre class="example">     foo = (char *) malloc (BUFFER_SIZE);

     </pre>



<p>then the C preprocessor will recognize and <dfn>expand</dfn> the macro

<code>BUFFER_SIZE</code>.  The C compiler will see the same tokens as it would

if you had written



<pre class="example">     foo = (char *) malloc (1024);

     </pre>



   <p>By convention, macro names are written in upper case.  Programs are

easier to read when it is possible to tell at a glance which names are

macros.



   <p>The macro's body ends at the end of the <code>#define</code> line.  You may

continue the definition onto multiple lines, if necessary, using

backslash-newline.  When the macro is expanded, however, it will all

come out on one line.  For example,



<pre class="example">     #define NUMBERS 1, \

                     2, \

                     3

     int x[] = { NUMBERS };

          ==&gt; int x[] = { 1, 2, 3 };

     </pre>



<p>The most common visible consequence of this is surprising line numbers

in error messages.



   <p>There is no restriction on what can go in a macro body provided it

decomposes into valid preprocessing tokens.  Parentheses need not

balance, and the body need not resemble valid C code.  (If it does not,

you may get error messages from the C compiler when you use the macro.)



   <p>The C preprocessor scans your program sequentially.  Macro definitions

take effect at the place you write them.  Therefore, the following input

to the C preprocessor



<pre class="example">     foo = X;

     #define X 4

     bar = X;

     </pre>



<p>produces



<pre class="example">     foo = X;

     bar = 4;

     </pre>



   <p>When the preprocessor expands a macro name, the macro's expansion

replaces the macro invocation, then the expansion is examined for more

macros to expand.  For example,



<pre class="example">     #define TABLESIZE BUFSIZE

     #define BUFSIZE 1024

     TABLESIZE

          ==&gt; BUFSIZE

          ==&gt; 1024

     </pre>



<p><code>TABLESIZE</code> is expanded first to produce <code>BUFSIZE</code>, then that

macro is expanded to produce the final result, <code>1024</code>.



   <p>Notice that <code>BUFSIZE</code> was not defined when <code>TABLESIZE</code> was

defined.  The <code>#define</code> for <code>TABLESIZE</code> uses exactly the

expansion you specify--in this case, <code>BUFSIZE</code>--and does not

check to see whether it too contains macro names.  Only when you

<em>use</em> <code>TABLESIZE</code> is the result of its expansion scanned for

more macro names.



   <p>This makes a difference if you change the definition of <code>BUFSIZE</code>

at some point in the source file.  <code>TABLESIZE</code>, defined as shown,

will always expand using the definition of <code>BUFSIZE</code> that is

currently in effect:



<pre class="example">     #define BUFSIZE 1020

     #define TABLESIZE BUFSIZE

     #undef BUFSIZE

     #define BUFSIZE 37

     </pre>



<p>Now <code>TABLESIZE</code> expands (in two stages) to <code>37</code>.



   <p>If the expansion of a macro contains its own name, either directly or

via intermediate macros, it is not expanded again when the expansion is

examined for more macros.  This prevents infinite recursion. 

See <a href="Self-Referential-Macros.html#Self-Referential%20Macros">Self-Referential Macros</a>, for the precise details.



   </body></html>



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -