📄 standard-predefined-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="Standard%20Predefined%20Macros">Standard Predefined Macros</a>,
Next:<a rel="next" accesskey="n" href="Common-Predefined-Macros.html#Common%20Predefined%20Macros">Common Predefined Macros</a>,
Up:<a rel="up" accesskey="u" href="Predefined-Macros.html#Predefined%20Macros">Predefined Macros</a>
<hr><br>
</div>
<h4 class="subsection">Standard Predefined Macros</h4>
<p>The standard predefined macros are specified by the C and/or C++
language standards, so they are available with all compilers that
implement those standards. Older compilers may not provide all of
them. Their names all start with double underscores.
<dl>
<dt><code>__FILE__</code>
<dd>This macro expands to the name of the current input file, in the form of
a C string constant. This is the path by which the preprocessor opened
the file, not the short name specified in <code>#include</code> or as the
input file name argument. For example,
<code>"/usr/local/include/myheader.h"</code> is a possible expansion of this
macro.
<br><dt><code>__LINE__</code>
<dd>This macro expands to the current input line number, in the form of a
decimal integer constant. While we call it a predefined macro, it's
a pretty strange macro, since its "definition" changes with each
new line of source code.
</dl>
<p><code>__FILE__</code> and <code>__LINE__</code> are useful in generating an error
message to report an inconsistency detected by the program; the message
can state the source line at which the inconsistency was detected. For
example,
<pre class="example"> fprintf (stderr, "Internal error: "
"negative string length "
"%d at %s, line %d.",
length, __FILE__, __LINE__);
</pre>
<p>An <code>#include</code> directive changes the expansions of <code>__FILE__</code>
and <code>__LINE__</code> to correspond to the included file. At the end of
that file, when processing resumes on the input file that contained
the <code>#include</code> directive, the expansions of <code>__FILE__</code> and
<code>__LINE__</code> revert to the values they had before the
<code>#include</code> (but <code>__LINE__</code> is then incremented by one as
processing moves to the line after the <code>#include</code>).
<p>A <code>#line</code> directive changes <code>__LINE__</code>, and may change
<code>__FILE__</code> as well. See <a href="Line-Control.html#Line%20Control">Line Control</a>.
<p>C99 introduces <code>__func__</code>, and GCC has provided <code>__FUNCTION__</code>
for a long time. Both of these are strings containing the name of the
current function (there are slight semantic differences; see the GCC
manual). Neither of them is a macro; the preprocessor does not know the
name of the current function. They tend to be useful in conjunction
with <code>__FILE__</code> and <code>__LINE__</code>, though.
<dl>
<br><dt><code>__DATE__</code>
<dd>This macro expands to a string constant that describes the date on which
the preprocessor is being run. The string constant contains eleven
characters and looks like <code>"Feb 12 1996"</code>. If the day of the
month is less than 10, it is padded with a space on the left.
<p>If GCC cannot determine the current date, it will emit a warning message
(once per compilation) and <code>__DATE__</code> will expand to
<code>"??? ?? ????"</code>.
<br><dt><code>__TIME__</code>
<dd>This macro expands to a string constant that describes the time at
which the preprocessor is being run. The string constant contains
eight characters and looks like <code>"23:59:01"</code>.
<p>If GCC cannot determine the current time, it will emit a warning message
(once per compilation) and <code>__TIME__</code> will expand to
<code>"??:??:??"</code>.
<br><dt><code>__STDC__</code>
<dd>In normal operation, this macro expands to the constant 1, to signify
that this compiler conforms to ISO Standard C. If GNU CPP is used with
a compiler other than GCC, this is not necessarily true; however, the
preprocessor always conforms to the standard unless the
<code>-traditional-cpp</code> option is used.
<p>This macro is not defined if the <code>-traditional-cpp</code> option is used.
<p>On some hosts, the system compiler uses a different convention, where
<code>__STDC__</code> is normally 0, but is 1 if the user specifies strict
conformance to the C Standard. CPP follows the host convention when
processing system header files, but when processing user files
<code>__STDC__</code> is always 1. This has been reported to cause problems;
for instance, some versions of Solaris provide X Windows headers that
expect <code>__STDC__</code> to be either undefined or 1. See <a href="Invocation.html#Invocation">Invocation</a>.
<br><dt><code>__STDC_VERSION__</code>
<dd>This macro expands to the C Standard's version number, a long integer
constant of the form <code></code><var>yyyy</var><code></code><var>mm</var><code>L</code> where <var>yyyy</var> and
<var>mm</var> are the year and month of the Standard version. This signifies
which version of the C Standard the compiler conforms to. Like
<code>__STDC__</code>, this is not necessarily accurate for the entire
implementation, unless GNU CPP is being used with GCC.
<p>The value <code>199409L</code> signifies the 1989 C standard as amended in
1994, which is the current default; the value <code>199901L</code> signifies
the 1999 revision of the C standard. Support for the 1999 revision is
not yet complete.
<p>This macro is not defined if the <code>-traditional-cpp</code> option is
used, nor when compiling C++ or Objective-C.
<br><dt><code>__STDC_HOSTED__</code>
<dd>This macro is defined, with value 1, if the compiler's target is a
<dfn>hosted environment</dfn>. A hosted environment has the complete
facilities of the standard C library available.
<br><dt><code>__cplusplus</code>
<dd>This macro is defined when the C++ compiler is in use. You can use
<code>__cplusplus</code> to test whether a header is compiled by a C compiler
or a C++ compiler. This macro is similar to <code>__STDC_VERSION__</code>, in
that it expands to a version number. A fully conforming implementation
of the 1998 C++ standard will define this macro to <code>199711L</code>. The
GNU C++ compiler is not yet fully conforming, so it uses <code>1</code>
instead. We hope to complete our implementation in the near future.
</dl>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -