diff options
Diffstat (limited to 'share/doc/cpp/Macro-Arguments.html')
-rw-r--r-- | share/doc/cpp/Macro-Arguments.html | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/share/doc/cpp/Macro-Arguments.html b/share/doc/cpp/Macro-Arguments.html new file mode 100644 index 0000000..33aaf33 --- /dev/null +++ b/share/doc/cpp/Macro-Arguments.html @@ -0,0 +1,193 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> +<html> +<!-- Copyright (C) 1987-2023 Free Software Foundation, Inc. + +Permission is granted to copy, distribute and/or modify this document +under the terms of the GNU Free Documentation License, Version 1.3 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". + +This manual contains no Invariant Sections. The Front-Cover Texts are +(a) (see below), and the Back-Cover Texts are (b) (see below). + +(a) The FSF's Front-Cover Text is: + +A GNU Manual + +(b) The FSF's Back-Cover Text is: + +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. --> +<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ --> +<head> +<title>The C Preprocessor: Macro Arguments</title> + +<meta name="description" content="The C Preprocessor: Macro Arguments"> +<meta name="keywords" content="The C Preprocessor: Macro Arguments"> +<meta name="resource-type" content="document"> +<meta name="distribution" content="global"> +<meta name="Generator" content="makeinfo"> +<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> +<link href="index.html#Top" rel="start" title="Top"> +<link href="Index-of-Directives.html#Index-of-Directives" rel="index" title="Index of Directives"> +<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> +<link href="Macros.html#Macros" rel="up" title="Macros"> +<link href="Stringizing.html#Stringizing" rel="next" title="Stringizing"> +<link href="Function_002dlike-Macros.html#Function_002dlike-Macros" rel="previous" title="Function-like Macros"> +<style type="text/css"> +<!-- +a.summary-letter {text-decoration: none} +blockquote.smallquotation {font-size: smaller} +div.display {margin-left: 3.2em} +div.example {margin-left: 3.2em} +div.indentedblock {margin-left: 3.2em} +div.lisp {margin-left: 3.2em} +div.smalldisplay {margin-left: 3.2em} +div.smallexample {margin-left: 3.2em} +div.smallindentedblock {margin-left: 3.2em; font-size: smaller} +div.smalllisp {margin-left: 3.2em} +kbd {font-style:oblique} +pre.display {font-family: inherit} +pre.format {font-family: inherit} +pre.menu-comment {font-family: serif} +pre.menu-preformatted {font-family: serif} +pre.smalldisplay {font-family: inherit; font-size: smaller} +pre.smallexample {font-size: smaller} +pre.smallformat {font-family: inherit; font-size: smaller} +pre.smalllisp {font-size: smaller} +span.nocodebreak {white-space:nowrap} +span.nolinebreak {white-space:nowrap} +span.roman {font-family:serif; font-weight:normal} +span.sansserif {font-family:sans-serif; font-weight:normal} +ul.no-bullet {list-style: none} +--> +</style> + + +</head> + +<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000"> +<a name="Macro-Arguments"></a> +<div class="header"> +<p> +Next: <a href="Stringizing.html#Stringizing" accesskey="n" rel="next">Stringizing</a>, Previous: <a href="Function_002dlike-Macros.html#Function_002dlike-Macros" accesskey="p" rel="previous">Function-like Macros</a>, Up: <a href="Macros.html#Macros" accesskey="u" rel="up">Macros</a> [<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> +</div> +<hr> +<a name="Macro-Arguments-1"></a> +<h3 class="section">3.3 Macro Arguments</h3> +<a name="index-arguments"></a> +<a name="index-macros-with-arguments"></a> +<a name="index-arguments-in-macro-definitions"></a> + +<p>Function-like macros can take <em>arguments</em>, just like true functions. +To define a macro that uses arguments, you insert <em>parameters</em> +between the pair of parentheses in the macro definition that make the +macro function-like. The parameters must be valid C identifiers, +separated by commas and optionally whitespace. +</p> +<p>To invoke a macro that takes arguments, you write the name of the macro +followed by a list of <em>actual arguments</em> in parentheses, separated +by commas. The invocation of the macro need not be restricted to a +single logical line—it can cross as many lines in the source file as +you wish. The number of arguments you give must match the number of +parameters in the macro definition. When the macro is expanded, each +use of a parameter in its body is replaced by the tokens of the +corresponding argument. (You need not use all of the parameters in the +macro body.) +</p> +<p>As an example, here is a macro that computes the minimum of two numeric +values, as it is defined in many C programs, and some uses. +</p> +<div class="smallexample"> +<pre class="smallexample">#define min(X, Y) ((X) < (Y) ? (X) : (Y)) + x = min(a, b); → x = ((a) < (b) ? (a) : (b)); + y = min(1, 2); → y = ((1) < (2) ? (1) : (2)); + z = min(a + 28, *p); → z = ((a + 28) < (*p) ? (a + 28) : (*p)); +</pre></div> + +<p>(In this small example you can already see several of the dangers of +macro arguments. See <a href="Macro-Pitfalls.html#Macro-Pitfalls">Macro Pitfalls</a>, for detailed explanations.) +</p> +<p>Leading and trailing whitespace in each argument is dropped, and all +whitespace between the tokens of an argument is reduced to a single +space. Parentheses within each argument must balance; a comma within +such parentheses does not end the argument. However, there is no +requirement for square brackets or braces to balance, and they do not +prevent a comma from separating arguments. Thus, +</p> +<div class="smallexample"> +<pre class="smallexample">macro (array[x = y, x + 1]) +</pre></div> + +<p>passes two arguments to <code>macro</code>: <code>array[x = y</code> and <code>x + +1]</code>. If you want to supply <code>array[x = y, x + 1]</code> as an argument, +you can write it as <code>array[(x = y, x + 1)]</code>, which is equivalent C +code. +</p> +<p>All arguments to a macro are completely macro-expanded before they are +substituted into the macro body. After substitution, the complete text +is scanned again for macros to expand, including the arguments. This rule +may seem strange, but it is carefully designed so you need not worry +about whether any function call is actually a macro invocation. You can +run into trouble if you try to be too clever, though. See <a href="Argument-Prescan.html#Argument-Prescan">Argument Prescan</a>, for detailed discussion. +</p> +<p>For example, <code>min (min (a, b), c)</code> is first expanded to +</p> +<div class="smallexample"> +<pre class="smallexample"> min (((a) < (b) ? (a) : (b)), (c)) +</pre></div> + +<p>and then to +</p> +<div class="smallexample"> +<pre class="smallexample">((((a) < (b) ? (a) : (b))) < (c) + ? (((a) < (b) ? (a) : (b))) + : (c)) +</pre></div> + +<p>(Line breaks shown here for clarity would not actually be generated.) +</p> +<a name="index-empty-macro-arguments"></a> +<p>You can leave macro arguments empty; this is not an error to the +preprocessor (but many macros will then expand to invalid code). +You cannot leave out arguments entirely; if a macro takes two arguments, +there must be exactly one comma at the top level of its argument list. +Here are some silly examples using <code>min</code>: +</p> +<div class="smallexample"> +<pre class="smallexample">min(, b) → (( ) < (b) ? ( ) : (b)) +min(a, ) → ((a ) < ( ) ? (a ) : ( )) +min(,) → (( ) < ( ) ? ( ) : ( )) +min((,),) → (((,)) < ( ) ? ((,)) : ( )) + +min() error→ macro "min" requires 2 arguments, but only 1 given +min(,,) error→ macro "min" passed 3 arguments, but takes just 2 +</pre></div> + +<p>Whitespace is not a preprocessing token, so if a macro <code>foo</code> takes +one argument, <code>foo ()<!-- /@w --></code> and <code>foo ( )<!-- /@w --></code> both supply it an +empty argument. Previous GNU preprocessor implementations and +documentation were incorrect on this point, insisting that a +function-like macro that takes a single argument be passed a space if an +empty argument was required. +</p> +<p>Macro parameters appearing inside string literals are not replaced by +their corresponding actual arguments. +</p> +<div class="smallexample"> +<pre class="smallexample">#define foo(x) x, "x" +foo(bar) → bar, "x" +</pre></div> + +<hr> +<div class="header"> +<p> +Next: <a href="Stringizing.html#Stringizing" accesskey="n" rel="next">Stringizing</a>, Previous: <a href="Function_002dlike-Macros.html#Function_002dlike-Macros" accesskey="p" rel="previous">Function-like Macros</a>, Up: <a href="Macros.html#Macros" accesskey="u" rel="up">Macros</a> [<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> +</div> + + + +</body> +</html> |