source: public/doc/gnu-c/Macro-Arguments.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 8.3 KB
Line 
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
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing 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<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.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>
60Next: <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> &nbsp; [<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.
69To define a macro that uses arguments, you insert <em>parameters</em>
70between the pair of parentheses in the macro definition that make the
71macro function-like. The parameters must be valid C identifiers,
72separated by commas and optionally whitespace.
73</p>
74<p>To invoke a macro that takes arguments, you write the name of the macro
75followed by a list of <em>actual arguments</em> in parentheses, separated
76by commas. The invocation of the macro need not be restricted to a
77single logical line&mdash;it can cross as many lines in the source file as
78you wish. The number of arguments you give must match the number of
79parameters in the macro definition. When the macro is expanded, each
80use of a parameter in its body is replaced by the tokens of the
81corresponding argument. (The macro body is not required to use all of the
82parameters.)
83</p>
84<p>As an example, here is a macro that computes the minimum of two numeric
85values, 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) &lt; (Y) ? (X) : (Y))
89 x = min(a, b); &rarr; x = ((a) &lt; (b) ? (a) : (b));
90 y = min(1, 2); &rarr; y = ((1) &lt; (2) ? (1) : (2));
91 z = min(a+28, *p); &rarr; z = ((a+28) &lt; (*p) ? (a+28) : (*p));
92</pre></div>
93
94<p>In this small example you can already see several of the dangers of
95macro 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
98whitespace between the tokens of an argument is reduced to a single
99space. Parentheses within each argument must balance; a comma within
100such parentheses does not end the argument. However, there is no
101requirement for square brackets or braces to balance, and they do not
102prevent 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 +
1091]</code>. If you want to supply <code>array[x = y, x + 1]</code> as an argument,
110you can write it as <code>array[(x = y, x + 1)]</code>, which is equivalent C
111code. However, putting an assignment inside an array subscript
112is to be avoided anyway.
113</p>
114<p>All arguments to a macro are completely macro-expanded before they are
115substituted into the macro body. After substitution, the complete text
116is scanned again for macros to expand, including the arguments. This rule
117may seem strange, but it is carefully designed so you need not worry
118about whether any function call is actually a macro invocation. You can
119run 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) &lt; (b) ? (a) : (b)), (c))
125</pre></div>
126
127<p>and then to
128</p>
129<div class="example">
130<pre class="example">((((a) &lt; (b) ? (a) : (b))) &lt; (c)
131 ? (((a) &lt; (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
139will then expand to invalid code. You cannot leave out arguments
140entirely; if a macro takes two arguments, there must be exactly one
141comma at the top level of its argument list. Here are some silly
142examples using <code>min</code>:
143</p>
144<div class="example">
145<pre class="example">min(, b) &rarr; (( ) &lt; (b) ? ( ) : (b))
146min(a, ) &rarr; ((a ) &lt; ( ) ? (a ) : ( ))
147min(,) &rarr; (( ) &lt; ( ) ? ( ) : ( ))
148min((,),) &rarr; (((,)) &lt; ( ) ? ((,)) : ( ))
149
150min() error&rarr; macro &quot;min&quot; requires 2 arguments, but only 1 given
151min(,,) error&rarr; macro &quot;min&quot; 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
155one argument, <code>foo&nbsp;()<!-- /@w --></code> and <code>foo&nbsp;(&nbsp;)<!-- /@w --></code> both supply it an
156empty argument.
157</p>
158
159<p>Macro parameters appearing inside string literals are not replaced by
160their corresponding actual arguments.
161</p>
162<div class="example">
163<pre class="example">#define foo(x) x, &quot;x&quot;
164foo(bar) &rarr; bar, &quot;x&quot;
165</pre></div>
166
167<p>See the next subsection for how to insert macro arguments
168into a string literal.
169</p>
170<p>The token following the macro call and the last token of the macro
171expansion 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
175foo()def &rarr; abc def
176</pre></div>
177
178<hr>
179<div class="header">
180<p>
181Next: <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> &nbsp; [<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>
Note: See TracBrowser for help on using the repository browser.