1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Macro Arguments (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Macro Arguments (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Macro Arguments (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Macros.html" rel="up" title="Macros">
|
---|
33 | <link href="Stringification.html" rel="next" title="Stringification">
|
---|
34 | <link href="Function_002dlike-Macros.html" rel="prev" title="Function-like Macros">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Macro-Arguments"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Stringification.html" accesskey="n" rel="next">Stringification</a>, Previous: <a href="Function_002dlike-Macros.html" accesskey="p" rel="prev">Function-like Macros</a>, Up: <a href="Macros.html" accesskey="u" rel="up">Macros</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Macro-Arguments-1"></span><h4 class="subsection">26.5.3 Macro Arguments</h4>
|
---|
64 | <span id="index-arguments"></span>
|
---|
65 | <span id="index-macros-with-arguments"></span>
|
---|
66 | <span id="index-arguments-in-macro-definitions"></span>
|
---|
67 |
|
---|
68 | <p>Function-like macros can take <em>arguments</em>, just like true functions.
|
---|
69 | To define a macro that uses arguments, you insert <em>parameters</em>
|
---|
70 | between the pair of parentheses in the macro definition that make the
|
---|
71 | macro function-like. The parameters must be valid C identifiers,
|
---|
72 | separated by commas and optionally whitespace.
|
---|
73 | </p>
|
---|
74 | <p>To invoke a macro that takes arguments, you write the name of the macro
|
---|
75 | followed by a list of <em>actual arguments</em> in parentheses, separated
|
---|
76 | by commas. The invocation of the macro need not be restricted to a
|
---|
77 | single logical line—it can cross as many lines in the source file as
|
---|
78 | you wish. The number of arguments you give must match the number of
|
---|
79 | parameters in the macro definition. When the macro is expanded, each
|
---|
80 | use of a parameter in its body is replaced by the tokens of the
|
---|
81 | corresponding argument. (The macro body is not required to use all of the
|
---|
82 | parameters.)
|
---|
83 | </p>
|
---|
84 | <p>As an example, here is a macro that computes the minimum of two numeric
|
---|
85 | values, as it is defined in many C programs, and some uses.
|
---|
86 | </p>
|
---|
87 | <div class="example">
|
---|
88 | <pre class="example">#define min(X, Y) ((X) < (Y) ? (X) : (Y))
|
---|
89 | x = min(a, b); → x = ((a) < (b) ? (a) : (b));
|
---|
90 | y = min(1, 2); → y = ((1) < (2) ? (1) : (2));
|
---|
91 | z = min(a+28, *p); → z = ((a+28) < (*p) ? (a+28) : (*p));
|
---|
92 | </pre></div>
|
---|
93 |
|
---|
94 | <p>In this small example you can already see several of the dangers of
|
---|
95 | macro arguments. See <a href="Macro-Pitfalls.html">Macro Pitfalls</a>, for detailed explanations.
|
---|
96 | </p>
|
---|
97 | <p>Leading and trailing whitespace in each argument is dropped, and all
|
---|
98 | whitespace between the tokens of an argument is reduced to a single
|
---|
99 | space. Parentheses within each argument must balance; a comma within
|
---|
100 | such parentheses does not end the argument. However, there is no
|
---|
101 | requirement for square brackets or braces to balance, and they do not
|
---|
102 | prevent a comma from separating arguments. Thus,
|
---|
103 | </p>
|
---|
104 | <div class="example">
|
---|
105 | <pre class="example">macro (array[x = y, x + 1])
|
---|
106 | </pre></div>
|
---|
107 |
|
---|
108 | <p>passes two arguments to <code>macro</code>: <code>array[x = y</code> and <code>x +
|
---|
109 | 1]</code>. If you want to supply <code>array[x = y, x + 1]</code> as an argument,
|
---|
110 | you can write it as <code>array[(x = y, x + 1)]</code>, which is equivalent C
|
---|
111 | code. However, putting an assignment inside an array subscript
|
---|
112 | is to be avoided anyway.
|
---|
113 | </p>
|
---|
114 | <p>All arguments to a macro are completely macro-expanded before they are
|
---|
115 | substituted into the macro body. After substitution, the complete text
|
---|
116 | is scanned again for macros to expand, including the arguments. This rule
|
---|
117 | may seem strange, but it is carefully designed so you need not worry
|
---|
118 | about whether any function call is actually a macro invocation. You can
|
---|
119 | run into trouble if you try to be too clever, though. See <a href="Argument-Prescan.html">Argument Prescan</a>, for detailed discussion.
|
---|
120 | </p>
|
---|
121 | <p>For example, <code>min (min (a, b), c)</code> is first expanded to
|
---|
122 | </p>
|
---|
123 | <div class="example">
|
---|
124 | <pre class="example"> min (((a) < (b) ? (a) : (b)), (c))
|
---|
125 | </pre></div>
|
---|
126 |
|
---|
127 | <p>and then to
|
---|
128 | </p>
|
---|
129 | <div class="example">
|
---|
130 | <pre class="example">((((a) < (b) ? (a) : (b))) < (c)
|
---|
131 | ? (((a) < (b) ? (a) : (b)))
|
---|
132 | : (c))
|
---|
133 | </pre></div>
|
---|
134 |
|
---|
135 | <p>(The line breaks shown here for clarity are not actually generated.)
|
---|
136 | </p>
|
---|
137 | <span id="index-empty-macro-arguments"></span>
|
---|
138 | <p>You can leave macro arguments empty without error, but many macros
|
---|
139 | will then expand to invalid code. You cannot leave out arguments
|
---|
140 | entirely; if a macro takes two arguments, there must be exactly one
|
---|
141 | comma at the top level of its argument list. Here are some silly
|
---|
142 | examples using <code>min</code>:
|
---|
143 | </p>
|
---|
144 | <div class="example">
|
---|
145 | <pre class="example">min(, b) → (( ) < (b) ? ( ) : (b))
|
---|
146 | min(a, ) → ((a ) < ( ) ? (a ) : ( ))
|
---|
147 | min(,) → (( ) < ( ) ? ( ) : ( ))
|
---|
148 | min((,),) → (((,)) < ( ) ? ((,)) : ( ))
|
---|
149 |
|
---|
150 | min() error→ macro "min" requires 2 arguments, but only 1 given
|
---|
151 | min(,,) error→ macro "min" passed 3 arguments, but takes just 2
|
---|
152 | </pre></div>
|
---|
153 |
|
---|
154 | <p>Whitespace is not a preprocessing token, so if a macro <code>foo</code> takes
|
---|
155 | one argument, <code>foo ()<!-- /@w --></code> and <code>foo ( )<!-- /@w --></code> both supply it an
|
---|
156 | empty argument.
|
---|
157 | </p>
|
---|
158 |
|
---|
159 | <p>Macro parameters appearing inside string literals are not replaced by
|
---|
160 | their corresponding actual arguments.
|
---|
161 | </p>
|
---|
162 | <div class="example">
|
---|
163 | <pre class="example">#define foo(x) x, "x"
|
---|
164 | foo(bar) → bar, "x"
|
---|
165 | </pre></div>
|
---|
166 |
|
---|
167 | <p>See the next subsection for how to insert macro arguments
|
---|
168 | into a string literal.
|
---|
169 | </p>
|
---|
170 | <p>The token following the macro call and the last token of the macro
|
---|
171 | expansion do not become one token even if it looks like they could:
|
---|
172 | </p>
|
---|
173 | <div class="example">
|
---|
174 | <pre class="example">#define foo() abc
|
---|
175 | foo()def → abc def
|
---|
176 | </pre></div>
|
---|
177 |
|
---|
178 | <hr>
|
---|
179 | <div class="header">
|
---|
180 | <p>
|
---|
181 | Next: <a href="Stringification.html" accesskey="n" rel="next">Stringification</a>, Previous: <a href="Function_002dlike-Macros.html" accesskey="p" rel="prev">Function-like Macros</a>, Up: <a href="Macros.html" accesskey="u" rel="up">Macros</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
182 | </div>
|
---|
183 |
|
---|
184 |
|
---|
185 |
|
---|
186 | </body>
|
---|
187 | </html>
|
---|