📄 function-like-macros.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 © 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="Function-like%20Macros">Function-like Macros</a>,
Next:<a rel="next" accesskey="n" href="Macro-Arguments.html#Macro%20Arguments">Macro Arguments</a>,
Previous:<a rel="previous" accesskey="p" href="Object-like-Macros.html#Object-like%20Macros">Object-like Macros</a>,
Up:<a rel="up" accesskey="u" href="Macros.html#Macros">Macros</a>
<hr><br>
</div>
<h3 class="section">Function-like Macros</h3>
<p>You can also define macros whose use looks like a function call. These
are called <dfn>function-like macros</dfn>. To define a function-like macro,
you use the same <code>#define</code> directive, but you put a pair of
parentheses immediately after the macro name. For example,
<pre class="example"> #define lang_init() c_init()
lang_init()
==> c_init()
</pre>
<p>A function-like macro is only expanded if its name appears with a pair
of parentheses after it. If you write just the name, it is left alone.
This can be useful when you have a function and a macro of the same
name, and you wish to use the function sometimes.
<pre class="example"> extern void foo(void);
#define foo() /* optimized inline version */
...
foo();
funcptr = foo;
</pre>
<p>Here the call to <code>foo()</code> will use the macro, but the function
pointer will get the address of the real function. If the macro were to
be expanded, it would cause a syntax error.
<p>If you put spaces between the macro name and the parentheses in the
macro definition, that does not define a function-like macro, it defines
an object-like macro whose expansion happens to begin with a pair of
parentheses.
<pre class="example"> #define lang_init () c_init()
lang_init()
==> () c_init()()
</pre>
<p>The first two pairs of parentheses in this expansion come from the
macro. The third is the pair that was originally after the macro
invocation. Since <code>lang_init</code> is an object-like macro, it does not
consume those parentheses.
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -