1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Predefined Macros (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Predefined Macros (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Predefined Macros (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Macros.html" rel="up" title="Macros">
|
---|
33 | <link href="Undefining-and-Redefining-Macros.html" rel="next" title="Undefining and Redefining Macros">
|
---|
34 | <link href="Variadic-Macros.html" rel="prev" title="Variadic Macros">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Predefined-Macros"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Undefining-and-Redefining-Macros.html" accesskey="n" rel="next">Undefining and Redefining Macros</a>, Previous: <a href="Variadic-Macros.html" accesskey="p" rel="prev">Variadic Macros</a>, Up: <a href="Macros.html" accesskey="u" rel="up">Macros</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Predefined-Macros-1"></span><h4 class="subsection">26.5.7 Predefined Macros</h4>
|
---|
64 | <span id="index-predefined-macros"></span>
|
---|
65 |
|
---|
66 | <p>Several object-like macros are predefined; you use them without
|
---|
67 | supplying their definitions. Here we explain the ones user programs
|
---|
68 | often need to use. Many other macro names starting with ‘<samp>__</samp>’ are
|
---|
69 | predefined; in general, you should not define such macro names
|
---|
70 | yourself.
|
---|
71 | </p>
|
---|
72 | <dl compact="compact">
|
---|
73 | <dt><code>__FILE__</code></dt>
|
---|
74 | <dd><p>This macro expands to the name of the current input file, in the form
|
---|
75 | of a C string constant. This is the full name by which the GCC opened
|
---|
76 | the file, not the short name specified in <code>#include</code> or as the
|
---|
77 | input file name argument. For example,
|
---|
78 | <code>"/usr/local/include/myheader.h"</code> is a possible expansion of this
|
---|
79 | macro.
|
---|
80 | </p>
|
---|
81 | </dd>
|
---|
82 | <dt><code>__LINE__</code></dt>
|
---|
83 | <dd><p>This macro expands to the current input line number, in the form of a
|
---|
84 | decimal integer constant. While we call it a predefined macro, it’s
|
---|
85 | a pretty strange macro, since its “definition” changes with each
|
---|
86 | new line of source code.
|
---|
87 | </p>
|
---|
88 | </dd>
|
---|
89 | <dt><code>__func__</code></dt>
|
---|
90 | <dt><code>__FUNCTION__</code></dt>
|
---|
91 | <dd><p>These names are like variables that have as value a string containing
|
---|
92 | the name of the current function definition. They are not really
|
---|
93 | macros, but this is the best place to mention them.
|
---|
94 | </p>
|
---|
95 | <p><code>__FUNCTION__</code> is the name that has been defined in GNU C since
|
---|
96 | time immemorial; <code>__func__</code> is defined by the C standard.
|
---|
97 | With the following conditionals, you can use whichever one is defined.
|
---|
98 | </p>
|
---|
99 | <div class="example">
|
---|
100 | <pre class="example">#if __STDC_VERSION__ < 199901L
|
---|
101 | # if __GNUC__ >= 2
|
---|
102 | # define __func__ __FUNCTION__
|
---|
103 | # else
|
---|
104 | # define __func__ "<unknown>"
|
---|
105 | # endif
|
---|
106 | #endif
|
---|
107 | </pre></div>
|
---|
108 |
|
---|
109 | </dd>
|
---|
110 | <dt><code>__PRETTY_FUNCTION__</code></dt>
|
---|
111 | <dd><p>This is equivalent to <code>__FUNCTION__</code> in C, but in C<code>++</code>
|
---|
112 | the string includes argument type information as well.
|
---|
113 | It is a GNU C extension.
|
---|
114 | </p></dd>
|
---|
115 | </dl>
|
---|
116 |
|
---|
117 | <p>Those features are useful in generating an error message to report an
|
---|
118 | inconsistency detected by the program; the message can state the
|
---|
119 | source line where the inconsistency was detected. For example,
|
---|
120 | </p>
|
---|
121 | <div class="example">
|
---|
122 | <pre class="example">fprintf (stderr, "Internal error: "
|
---|
123 | "negative string length "
|
---|
124 | "in function %s "
|
---|
125 | "%d at %s, line %d.",
|
---|
126 | __func__, length, __FILE__, __LINE__);
|
---|
127 | </pre></div>
|
---|
128 |
|
---|
129 | <p>A <code>#line</code> directive changes <code>__LINE__</code>, and may change
|
---|
130 | <code>__FILE__</code> as well. See <a href="Line-Control.html">Line Control</a>.
|
---|
131 | </p>
|
---|
132 | <dl compact="compact">
|
---|
133 | <dt><code>__DATE__</code></dt>
|
---|
134 | <dd><p>This macro expands to a string constant that describes the date of
|
---|
135 | compilation. The string constant contains eleven characters and looks
|
---|
136 | like <code>"Feb 12 1996"<!-- /@w --></code>. If the day of the month is just one
|
---|
137 | digit, an extra space precedes it so that the date is always eleven
|
---|
138 | characters.
|
---|
139 | </p>
|
---|
140 | <p>If the compiler cannot determine the current date, it emits a warning messages
|
---|
141 | (once per compilation) and <code>__DATE__</code> expands to
|
---|
142 | <code>"??? ?? ????"<!-- /@w --></code>.
|
---|
143 | </p>
|
---|
144 | <p>We deprecate the use of <code>__DATE__</code> for the sake of reproducible
|
---|
145 | compilation.
|
---|
146 | </p>
|
---|
147 | </dd>
|
---|
148 | <dt><code>__TIME__</code></dt>
|
---|
149 | <dd><p>This macro expands to a string constant that describes the time of
|
---|
150 | compilation. The string constant contains eight characters and looks
|
---|
151 | like <code>"23:59:01"</code>.
|
---|
152 | </p>
|
---|
153 | <p>If the compiler cannot determine the current time, it emits a warning
|
---|
154 | message (once per compilation) and <code>__TIME__</code> expands to
|
---|
155 | <code>"??:??:??"</code>.
|
---|
156 | </p>
|
---|
157 | <p>We deprecate the use of <code>__TIME__</code> for the sake of reproducible
|
---|
158 | compilation.
|
---|
159 | </p>
|
---|
160 | </dd>
|
---|
161 | <dt><code>__STDC__</code></dt>
|
---|
162 | <dd><p>In normal operation, this macro expands to the constant 1, to signify
|
---|
163 | that this compiler implements ISO Standard C.
|
---|
164 | </p>
|
---|
165 | </dd>
|
---|
166 | <dt><code>__STDC_VERSION__</code></dt>
|
---|
167 | <dd><p>This macro expands to the C Standard’s version number, a long integer
|
---|
168 | constant of the form <code><var>yyyy</var><var>mm</var>L</code> where <var>yyyy</var> and
|
---|
169 | <var>mm</var> are the year and month of the Standard version. This states
|
---|
170 | which version of the C Standard the compiler implements.
|
---|
171 | </p>
|
---|
172 | <p>The current default value is <code>201112L</code>, which signifies the C
|
---|
173 | 2011 standard.
|
---|
174 | </p>
|
---|
175 | </dd>
|
---|
176 | <dt><code>__STDC_HOSTED__</code></dt>
|
---|
177 | <dd><p>This macro is defined, with value 1, if the compiler’s target is a
|
---|
178 | <em>hosted environment</em>. A hosted environment provides the full
|
---|
179 | facilities of the standard C library.
|
---|
180 | </p></dd>
|
---|
181 | </dl>
|
---|
182 |
|
---|
183 | <p>The rest of the predefined macros are GNU C extensions.
|
---|
184 | </p>
|
---|
185 | <dl compact="compact">
|
---|
186 | <dt><code>__COUNTER__</code></dt>
|
---|
187 | <dd><p>This macro expands to sequential integral values starting from 0. In
|
---|
188 | other words, each time the program uses this acro, it generates the
|
---|
189 | next successive integer. This, with the <code>##</code> operator, provides
|
---|
190 | a convenient means for macros to generate unique identifiers.
|
---|
191 | </p>
|
---|
192 | </dd>
|
---|
193 | <dt><code>__GNUC__</code></dt>
|
---|
194 | <dt><code>__GNUC_MINOR__</code></dt>
|
---|
195 | <dt><code>__GNUC_PATCHLEVEL__</code></dt>
|
---|
196 | <dd><p>These macros expand to the major version, minor version, and patch
|
---|
197 | level of the compiler, as integer constants. For example, GCC 3.2.1
|
---|
198 | expands <code>__GNUC__</code> to 3, <code>__GNUC_MINOR__</code> to 2, and
|
---|
199 | <code>__GNUC_PATCHLEVEL__</code> to 1.
|
---|
200 | </p>
|
---|
201 | <p>If all you need to know is whether or not your program is being
|
---|
202 | compiled by GCC, or a non-GCC compiler that claims to accept the GNU C
|
---|
203 | extensions, you can simply test <code>__GNUC__</code>. If you need to write
|
---|
204 | code that depends on a specific version, you must check more
|
---|
205 | carefully. Each change in the minor version resets the patch level to
|
---|
206 | zero; each change in the major version (which happens rarely) resets
|
---|
207 | the minor version and the patch level to zero. To use the predefined
|
---|
208 | macros directly in the conditional, write it like this:
|
---|
209 | </p>
|
---|
210 | <div class="example">
|
---|
211 | <pre class="example">/* <span class="roman">Test for version 3.2.0 or later.</span> */
|
---|
212 | #if __GNUC__ > 3 || \
|
---|
213 | (__GNUC__ == 3 && (__GNUC_MINOR__ > 2 || \
|
---|
214 | (__GNUC_MINOR__ == 2 && \
|
---|
215 | __GNUC_PATCHLEVEL__ > 0))
|
---|
216 | </pre></div>
|
---|
217 |
|
---|
218 | <p>Another approach is to use the predefined macros to
|
---|
219 | calculate a single number, then compare that against a threshold:
|
---|
220 | </p>
|
---|
221 | <div class="example">
|
---|
222 | <pre class="example">#define GCC_VERSION (__GNUC__ * 10000 \
|
---|
223 | + __GNUC_MINOR__ * 100 \
|
---|
224 | + __GNUC_PATCHLEVEL__)
|
---|
225 | /* <span class="roman">…</span> */
|
---|
226 | /* <span class="roman">Test for GCC > 3.2.0</span> */
|
---|
227 | #if GCC_VERSION > 30200
|
---|
228 | </pre></div>
|
---|
229 |
|
---|
230 | <p>Many people find this form easier to understand.
|
---|
231 | </p>
|
---|
232 | </dd>
|
---|
233 | <dt><code>__VERSION__</code></dt>
|
---|
234 | <dd><p>This macro expands to a string constant that describes the version of
|
---|
235 | the compiler in use. You should not rely on its contents’ having any
|
---|
236 | particular form, but you can count on it to contain at least the
|
---|
237 | release number.
|
---|
238 | </p>
|
---|
239 | </dd>
|
---|
240 | <dt><code>__TIMESTAMP__</code></dt>
|
---|
241 | <dd><p>This macro expands to a string constant that describes the date and
|
---|
242 | time of the last modification of the current source file. The string
|
---|
243 | constant contains abbreviated day of the week, month, day of the
|
---|
244 | month, time in hh:mm:ss form, and the year, in the format
|
---|
245 | <code>"Sun Sep 16 01:03:52 1973"<!-- /@w --></code>. If the day of the month is
|
---|
246 | less than 10, it is padded with a space on the left.
|
---|
247 | </p>
|
---|
248 | <p>If GCC cannot determine that information date, it emits a warning
|
---|
249 | message (once per compilation) and <code>__TIMESTAMP__</code> expands to
|
---|
250 | <code>"??? ??? ?? ??:??:?? ????"<!-- /@w --></code>.
|
---|
251 | </p>
|
---|
252 | <p>We deprecate the use of this macro for the sake of reproducible
|
---|
253 | compilation.
|
---|
254 | </p></dd>
|
---|
255 | </dl>
|
---|
256 |
|
---|
257 | <hr>
|
---|
258 | <div class="header">
|
---|
259 | <p>
|
---|
260 | Next: <a href="Undefining-and-Redefining-Macros.html" accesskey="n" rel="next">Undefining and Redefining Macros</a>, Previous: <a href="Variadic-Macros.html" accesskey="p" rel="prev">Variadic Macros</a>, Up: <a href="Macros.html" accesskey="u" rel="up">Macros</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
261 | </div>
|
---|
262 |
|
---|
263 |
|
---|
264 |
|
---|
265 | </body>
|
---|
266 | </html>
|
---|