📄 function-names.html
字号:
<html lang="en">
<head>
<title>Using the GNU Compiler Collection (GCC)</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Using the GNU Compiler Collection (GCC)">
<meta name="generator" content="makeinfo 4.3">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home">
<!--
Copyright © 1988, 1989, 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.2 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "GNU General Public License" and "Funding
Free Software", the Front-Cover texts being (a) (see below), and with
the Back-Cover Texts being (b) (see below). A copy of the license is
included in the section entitled "GNU Free Documentation License".
<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%20Names">Function Names</a>,
Next:<a rel="next" accesskey="n" href="Return-Address.html#Return%20Address">Return Address</a>,
Previous:<a rel="previous" accesskey="p" href="Incomplete-Enums.html#Incomplete%20Enums">Incomplete Enums</a>,
Up:<a rel="up" accesskey="u" href="C-Extensions.html#C%20Extensions">C Extensions</a>
<hr><br>
</div>
<h3 class="section">Function Names as Strings</h3>
<p>GCC predefines two magic identifiers to hold the name of the current
function. The identifier <code>__FUNCTION__</code> holds the name of the function
as it appears in the source. The identifier <code>__PRETTY_FUNCTION__</code>
holds the name of the function pretty printed in a language specific
fashion.
<p>These names are always the same in a C function, but in a C++ function
they may be different. For example, this program:
<pre class="smallexample"> extern "C" {
extern int printf (char *, ...);
}
class a {
public:
sub (int i)
{
printf ("__FUNCTION__ = %s\n", __FUNCTION__);
printf ("__PRETTY_FUNCTION__ = %s\n", __PRETTY_FUNCTION__);
}
};
int
main (void)
{
a ax;
ax.sub (0);
return 0;
}
</pre>
<p>gives this output:
<pre class="smallexample"> __FUNCTION__ = sub
__PRETTY_FUNCTION__ = int a::sub (int)
</pre>
<p>The compiler automagically replaces the identifiers with a string
literal containing the appropriate name. Thus, they are neither
preprocessor macros, like <code>__FILE__</code> and <code>__LINE__</code>, nor
variables. This means that they catenate with other string literals, and
that they can be used to initialize char arrays. For example
<pre class="smallexample"> char here[] = "Function " __FUNCTION__ " in " __FILE__;
</pre>
<p>On the other hand, <code>#ifdef __FUNCTION__</code> does not have any special
meaning inside a function, since the preprocessor does not do anything
special with the identifier <code>__FUNCTION__</code>.
<p>Note that these semantics are deprecated, and that GCC 3.2 will handle
<code>__FUNCTION__</code> and <code>__PRETTY_FUNCTION__</code> the same way as
<code>__func__</code>. <code>__func__</code> is defined by the ISO standard C99:
<pre class="display"> The identifier <code>__func__</code> is implicitly declared by the translator
as if, immediately following the opening brace of each function
definition, the declaration
<pre class="smallexample"> static const char __func__[] = "function-name";
</pre>
appeared, where function-name is the name of the lexically-enclosing
function. This name is the unadorned name of the function.
</pre>
<p>By this definition, <code>__func__</code> is a variable, not a string literal.
In particular, <code>__func__</code> does not catenate with other string
literals.
<p>In <code>C++</code>, <code>__FUNCTION__</code> and <code>__PRETTY_FUNCTION__</code> are
variables, declared in the same way as <code>__func__</code>.
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -