source: public/doc/gnu-c/Predefined-Macros.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 11.2 KB
Line 
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
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing 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<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.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>
60Next: <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> &nbsp; [<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
67supplying their definitions. Here we explain the ones user programs
68often need to use. Many other macro names starting with &lsquo;<samp>__</samp>&rsquo; are
69predefined; in general, you should not define such macro names
70yourself.
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
75of a C string constant. This is the full name by which the GCC opened
76the file, not the short name specified in <code>#include</code> or as the
77input file name argument. For example,
78<code>&quot;/usr/local/include/myheader.h&quot;</code> is a possible expansion of this
79macro.
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
84decimal integer constant. While we call it a predefined macro, it&rsquo;s
85a pretty strange macro, since its &ldquo;definition&rdquo; changes with each
86new 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
92the name of the current function definition. They are not really
93macros, 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
96time immemorial; <code>__func__</code> is defined by the C standard.
97With the following conditionals, you can use whichever one is defined.
98</p>
99<div class="example">
100<pre class="example">#if __STDC_VERSION__ &lt; 199901L
101# if __GNUC__ &gt;= 2
102# define __func__ __FUNCTION__
103# else
104# define __func__ &quot;&lt;unknown&gt;&quot;
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>
112the string includes argument type information as well.
113It 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
118inconsistency detected by the program; the message can state the
119source line where the inconsistency was detected. For example,
120</p>
121<div class="example">
122<pre class="example">fprintf (stderr, &quot;Internal error: &quot;
123 &quot;negative string length &quot;
124 &quot;in function %s &quot;
125 &quot;%d at %s, line %d.&quot;,
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
135compilation. The string constant contains eleven characters and looks
136like <code>&quot;Feb&nbsp;12&nbsp;1996&quot;<!-- /@w --></code>. If the day of the month is just one
137digit, an extra space precedes it so that the date is always eleven
138characters.
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>&quot;???&nbsp;??&nbsp;????&quot;<!-- /@w --></code>.
143</p>
144<p>We deprecate the use of <code>__DATE__</code> for the sake of reproducible
145compilation.
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
150compilation. The string constant contains eight characters and looks
151like <code>&quot;23:59:01&quot;</code>.
152</p>
153<p>If the compiler cannot determine the current time, it emits a warning
154message (once per compilation) and <code>__TIME__</code> expands to
155<code>&quot;??:??:??&quot;</code>.
156</p>
157<p>We deprecate the use of <code>__TIME__</code> for the sake of reproducible
158compilation.
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
163that 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&rsquo;s version number, a long integer
168constant 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
170which version of the C Standard the compiler implements.
171</p>
172<p>The current default value is <code>201112L</code>, which signifies the C
1732011 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&rsquo;s target is a
178<em>hosted environment</em>. A hosted environment provides the full
179facilities 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
188other words, each time the program uses this acro, it generates the
189next successive integer. This, with the <code>##</code> operator, provides
190a 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
197level of the compiler, as integer constants. For example, GCC 3.2.1
198expands <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
202compiled by GCC, or a non-GCC compiler that claims to accept the GNU C
203extensions, you can simply test <code>__GNUC__</code>. If you need to write
204code that depends on a specific version, you must check more
205carefully. Each change in the minor version resets the patch level to
206zero; each change in the major version (which happens rarely) resets
207the minor version and the patch level to zero. To use the predefined
208macros 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__ &gt; 3 || \
213 (__GNUC__ == 3 &amp;&amp; (__GNUC_MINOR__ &gt; 2 || \
214 (__GNUC_MINOR__ == 2 &amp;&amp; \
215 __GNUC_PATCHLEVEL__ &gt; 0))
216</pre></div>
217
218<p>Another approach is to use the predefined macros to
219calculate 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">&hellip;</span> */
226/* <span class="roman">Test for GCC &gt; 3.2.0</span> */
227#if GCC_VERSION &gt; 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
235the compiler in use. You should not rely on its contents&rsquo; having any
236particular form, but you can count on it to contain at least the
237release 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
242time of the last modification of the current source file. The string
243constant contains abbreviated day of the week, month, day of the
244month, time in hh:mm:ss form, and the year, in the format
245<code>&quot;Sun&nbsp;Sep&nbsp;16&nbsp;01:03:52&nbsp;1973&quot;<!-- /@w --></code>. If the day of the month is
246less 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
249message (once per compilation) and <code>__TIMESTAMP__</code> expands to
250<code>&quot;???&nbsp;???&nbsp;??&nbsp;??:??:??&nbsp;????&quot;<!-- /@w --></code>.
251</p>
252<p>We deprecate the use of this macro for the sake of reproducible
253compilation.
254</p></dd>
255</dl>
256
257<hr>
258<div class="header">
259<p>
260Next: <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> &nbsp; [<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>
Note: See TracBrowser for help on using the repository browser.