You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
8.2 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Copyright (C) 1987-2018 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 6.4, http://www.gnu.org/software/texinfo/ -->
<head>
<title>Traditional macros (The C Preprocessor)</title>
<meta name="description" content="Traditional macros (The C Preprocessor)">
<meta name="keywords" content="Traditional macros (The C Preprocessor)">
<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="Traditional-Mode.html#Traditional-Mode" rel="up" title="Traditional Mode">
<link href="Traditional-miscellany.html#Traditional-miscellany" rel="next" title="Traditional miscellany">
<link href="Traditional-lexical-analysis.html#Traditional-lexical-analysis" rel="prev" title="Traditional lexical analysis">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
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.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en">
<a name="Traditional-macros"></a>
<div class="header">
<p>
Next: <a href="Traditional-miscellany.html#Traditional-miscellany" accesskey="n" rel="next">Traditional miscellany</a>, Previous: <a href="Traditional-lexical-analysis.html#Traditional-lexical-analysis" accesskey="p" rel="prev">Traditional lexical analysis</a>, Up: <a href="Traditional-Mode.html#Traditional-Mode" accesskey="u" rel="up">Traditional Mode</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>
</div>
<hr>
<a name="Traditional-macros-1"></a>
<h3 class="section">10.2 Traditional macros</h3>
<p>The major difference between traditional and ISO macros is that the
former expand to text rather than to a token sequence. CPP removes
all leading and trailing horizontal whitespace from a macro&rsquo;s
replacement text before storing it, but preserves the form of internal
whitespace.
</p>
<p>One consequence is that it is legitimate for the replacement text to
contain an unmatched quote (see <a href="Traditional-lexical-analysis.html#Traditional-lexical-analysis">Traditional lexical analysis</a>). An
unclosed string or character constant continues into the text
following the macro call. Similarly, the text at the end of a macro&rsquo;s
expansion can run together with the text after the macro invocation to
produce a single token.
</p>
<p>Normally comments are removed from the replacement text after the
macro is expanded, but if the <samp>-CC</samp> option is passed on the
command-line comments are preserved. (In fact, the current
implementation removes comments even before saving the macro
replacement text, but it careful to do it in such a way that the
observed effect is identical even in the function-like macro case.)
</p>
<p>The ISO stringizing operator &lsquo;<samp>#</samp>&rsquo; and token paste operator
&lsquo;<samp>##</samp>&rsquo; have no special meaning. As explained later, an effect
similar to these operators can be obtained in a different way. Macro
names that are embedded in quotes, either from the main file or after
macro replacement, do not expand.
</p>
<p>CPP replaces an unquoted object-like macro name with its replacement
text, and then rescans it for further macros to replace. Unlike
standard macro expansion, traditional macro expansion has no provision
to prevent recursion. If an object-like macro appears unquoted in its
replacement text, it will be replaced again during the rescan pass,
and so on <em>ad infinitum</em>. GCC detects when it is expanding
recursive macros, emits an error message, and continues after the
offending macro invocation.
</p>
<div class="smallexample">
<pre class="smallexample">#define PLUS +
#define INC(x) PLUS+x
INC(foo);
&rarr; ++foo;
</pre></div>
<p>Function-like macros are similar in form but quite different in
behavior to their ISO counterparts. Their arguments are contained
within parentheses, are comma-separated, and can cross physical lines.
Commas within nested parentheses are not treated as argument
separators. Similarly, a quote in an argument cannot be left
unclosed; a following comma or parenthesis that comes before the
closing quote is treated like any other character. There is no
facility for handling variadic macros.
</p>
<p>This implementation removes all comments from macro arguments, unless
the <samp>-C</samp> option is given. The form of all other horizontal
whitespace in arguments is preserved, including leading and trailing
whitespace. In particular
</p>
<div class="smallexample">
<pre class="smallexample">f( )
</pre></div>
<p>is treated as an invocation of the macro &lsquo;<samp>f</samp>&rsquo; with a single
argument consisting of a single space. If you want to invoke a
function-like macro that takes no arguments, you must not leave any
whitespace between the parentheses.
</p>
<p>If a macro argument crosses a new line, the new line is replaced with
a space when forming the argument. If the previous line contained an
unterminated quote, the following line inherits the quoted state.
</p>
<p>Traditional preprocessors replace parameters in the replacement text
with their arguments regardless of whether the parameters are within
quotes or not. This provides a way to stringize arguments. For
example
</p>
<div class="smallexample">
<pre class="smallexample">#define str(x) &quot;x&quot;
str(/* <span class="roman">A comment</span> */some text )
&rarr; &quot;some text &quot;
</pre></div>
<p>Note that the comment is removed, but that the trailing space is
preserved. Here is an example of using a comment to effect token
pasting.
</p>
<div class="smallexample">
<pre class="smallexample">#define suffix(x) foo_/**/x
suffix(bar)
&rarr; foo_bar
</pre></div>
<hr>
<div class="header">
<p>
Next: <a href="Traditional-miscellany.html#Traditional-miscellany" accesskey="n" rel="next">Traditional miscellany</a>, Previous: <a href="Traditional-lexical-analysis.html#Traditional-lexical-analysis" accesskey="p" rel="prev">Traditional lexical analysis</a>, Up: <a href="Traditional-Mode.html#Traditional-Mode" accesskey="u" rel="up">Traditional Mode</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>
</div>
</body>
</html>