blob: efbb6ccb3c2d06298f93560399277c080ef6c020 [file] [log] [blame]
<html lang="en">
<head>
<title>Macro Definitions - The GNU C Library</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="The GNU C Library">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Using-the-Library.html#Using-the-Library" title="Using the Library">
<link rel="prev" href="Header-Files.html#Header-Files" title="Header Files">
<link rel="next" href="Reserved-Names.html#Reserved-Names" title="Reserved Names">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
This file documents the GNU C library.
This is Edition 0.12, last updated 2007-10-27,
of `The GNU C Library Reference Manual', for version
2.8 (Sourcery G++ Lite 2011.03-41).
Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
2003, 2007, 2008, 2010 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; with the
Invariant Sections being ``Free Software Needs Free Documentation''
and ``GNU Lesser General Public License'', the Front-Cover texts being
``A GNU Manual'', and with the Back-Cover Texts as in (a) below. A
copy of the license is included in the section entitled "GNU Free
Documentation License".
(a) The FSF's Back-Cover Text is: ``You have the freedom to
copy and modify this GNU manual. Buying copies from the FSF
supports it in developing GNU and promoting software freedom.''-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
<link rel="stylesheet" type="text/css" href="../cs.css">
</head>
<body>
<div class="node">
<a name="Macro-Definitions"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Reserved-Names.html#Reserved-Names">Reserved Names</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Header-Files.html#Header-Files">Header Files</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Using-the-Library.html#Using-the-Library">Using the Library</a>
<hr>
</div>
<h4 class="subsection">1.3.2 Macro Definitions of Functions</h4>
<p><a name="index-shadowing-functions-with-macros-23"></a><a name="index-removing-macros-that-shadow-functions-24"></a><a name="index-undefining-macros-that-shadow-functions-25"></a>
If we describe something as a function in this manual, it may have a
macro definition as well. This normally has no effect on how your
program runs&mdash;the macro definition does the same thing as the function
would. In particular, macro equivalents for library functions evaluate
arguments exactly once, in the same way that a function call would. The
main reason for these macro definitions is that sometimes they can
produce an inline expansion that is considerably faster than an actual
function call.
<p>Taking the address of a library function works even if it is also
defined as a macro. This is because, in this context, the name of the
function isn't followed by the left parenthesis that is syntactically
necessary to recognize a macro call.
<p>You might occasionally want to avoid using the macro definition of a
function&mdash;perhaps to make your program easier to debug. There are
two ways you can do this:
<ul>
<li>You can avoid a macro definition in a specific use by enclosing the name
of the function in parentheses. This works because the name of the
function doesn't appear in a syntactic context where it is recognizable
as a macro call.
<li>You can suppress any macro definition for a whole source file by using
the &lsquo;<samp><span class="samp">#undef</span></samp>&rsquo; preprocessor directive, unless otherwise stated
explicitly in the description of that facility.
</ul>
<p>For example, suppose the header file <samp><span class="file">stdlib.h</span></samp> declares a function
named <code>abs</code> with
<pre class="smallexample"> extern int abs (int);
</pre>
<p class="noindent">and also provides a macro definition for <code>abs</code>. Then, in:
<pre class="smallexample"> #include &lt;stdlib.h&gt;
int f (int *i) { return abs (++*i); }
</pre>
<p class="noindent">the reference to <code>abs</code> might refer to either a macro or a function.
On the other hand, in each of the following examples the reference is
to a function and not a macro.
<pre class="smallexample"> #include &lt;stdlib.h&gt;
int g (int *i) { return (abs) (++*i); }
#undef abs
int h (int *i) { return abs (++*i); }
</pre>
<p>Since macro definitions that double for a function behave in
exactly the same way as the actual function version, there is usually no
need for any of these methods. In fact, removing macro definitions usually
just makes your program slower.
</body></html>