Standard-Predefined-Macros.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1987-2023 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation. A copy of
  7. the license is included in the
  8. section entitled "GNU Free Documentation License".
  9. This manual contains no Invariant Sections. The Front-Cover Texts are
  10. (a) (see below), and the Back-Cover Texts are (b) (see below).
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>Standard Predefined Macros (The C Preprocessor)</title>
  21. <meta name="description" content="Standard Predefined Macros (The C Preprocessor)">
  22. <meta name="keywords" content="Standard Predefined Macros (The C Preprocessor)">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  26. <link href="index.html" rel="start" title="Top">
  27. <link href="Index-of-Directives.html" rel="index" title="Index of Directives">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Predefined-Macros.html" rel="up" title="Predefined Macros">
  30. <link href="Common-Predefined-Macros.html" rel="next" title="Common Predefined Macros">
  31. <link href="Predefined-Macros.html" rel="prev" title="Predefined Macros">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.indentedblock {margin-right: 0em}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.lisp {margin-left: 3.2em}
  39. kbd {font-style: oblique}
  40. pre.display {font-family: inherit}
  41. pre.format {font-family: inherit}
  42. pre.menu-comment {font-family: serif}
  43. pre.menu-preformatted {font-family: serif}
  44. span.nolinebreak {white-space: nowrap}
  45. span.roman {font-family: initial; font-weight: normal}
  46. span.sansserif {font-family: sans-serif; font-weight: normal}
  47. ul.no-bullet {list-style: none}
  48. -->
  49. </style>
  50. </head>
  51. <body lang="en">
  52. <span id="Standard-Predefined-Macros"></span><div class="header">
  53. <p>
  54. Next: <a href="Common-Predefined-Macros.html" accesskey="n" rel="next">Common Predefined Macros</a>, Up: <a href="Predefined-Macros.html" accesskey="u" rel="up">Predefined Macros</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index-of-Directives.html" title="Index" rel="index">Index</a>]</p>
  55. </div>
  56. <hr>
  57. <span id="Standard-Predefined-Macros-1"></span><h4 class="subsection">3.7.1 Standard Predefined Macros</h4>
  58. <span id="index-standard-predefined-macros_002e"></span>
  59. <p>The standard predefined macros are specified by the relevant
  60. language standards, so they are available with all compilers that
  61. implement those standards. Older compilers may not provide all of
  62. them. Their names all start with double underscores.
  63. </p>
  64. <dl compact="compact">
  65. <dt><code>__FILE__</code></dt>
  66. <dd><p>This macro expands to the name of the current input file, in the form of
  67. a C string constant. This is the path by which the preprocessor opened
  68. the file, not the short name specified in &lsquo;<samp>#include</samp>&rsquo; or as the
  69. input file name argument. For example,
  70. <code>&quot;/usr/local/include/myheader.h&quot;</code> is a possible expansion of this
  71. macro.
  72. </p>
  73. </dd>
  74. <dt><code>__LINE__</code></dt>
  75. <dd><p>This macro expands to the current input line number, in the form of a
  76. decimal integer constant. While we call it a predefined macro, it&rsquo;s
  77. a pretty strange macro, since its &ldquo;definition&rdquo; changes with each
  78. new line of source code.
  79. </p></dd>
  80. </dl>
  81. <p><code>__FILE__</code> and <code>__LINE__</code> are useful in generating an error
  82. message to report an inconsistency detected by the program; the message
  83. can state the source line at which the inconsistency was detected. For
  84. example,
  85. </p>
  86. <div class="example">
  87. <pre class="example">fprintf (stderr, &quot;Internal error: &quot;
  88. &quot;negative string length &quot;
  89. &quot;%d at %s, line %d.&quot;,
  90. length, __FILE__, __LINE__);
  91. </pre></div>
  92. <p>An &lsquo;<samp>#include</samp>&rsquo; directive changes the expansions of <code>__FILE__</code>
  93. and <code>__LINE__</code> to correspond to the included file. At the end of
  94. that file, when processing resumes on the input file that contained
  95. the &lsquo;<samp>#include</samp>&rsquo; directive, the expansions of <code>__FILE__</code> and
  96. <code>__LINE__</code> revert to the values they had before the
  97. &lsquo;<samp>#include</samp>&rsquo; (but <code>__LINE__</code> is then incremented by one as
  98. processing moves to the line after the &lsquo;<samp>#include</samp>&rsquo;).
  99. </p>
  100. <p>A &lsquo;<samp>#line</samp>&rsquo; directive changes <code>__LINE__</code>, and may change
  101. <code>__FILE__</code> as well. See <a href="Line-Control.html">Line Control</a>.
  102. </p>
  103. <p>C99 introduced <code>__func__</code>, and GCC has provided <code>__FUNCTION__</code>
  104. for a long time. Both of these are strings containing the name of the
  105. current function (there are slight semantic differences; see the GCC
  106. manual). Neither of them is a macro; the preprocessor does not know the
  107. name of the current function. They tend to be useful in conjunction
  108. with <code>__FILE__</code> and <code>__LINE__</code>, though.
  109. </p>
  110. <dl compact="compact">
  111. <dt><code>__DATE__</code></dt>
  112. <dd><p>This macro expands to a string constant that describes the date on which
  113. the preprocessor is being run. The string constant contains eleven
  114. characters and looks like <code>&quot;Feb&nbsp;12&nbsp;1996&quot;<!-- /@w --></code>. If the day of the
  115. month is less than 10, it is padded with a space on the left.
  116. </p>
  117. <p>If GCC cannot determine the current date, it will emit a warning message
  118. (once per compilation) and <code>__DATE__</code> will expand to
  119. <code>&quot;???&nbsp;??&nbsp;????&quot;<!-- /@w --></code>.
  120. </p>
  121. </dd>
  122. <dt><code>__TIME__</code></dt>
  123. <dd><p>This macro expands to a string constant that describes the time at
  124. which the preprocessor is being run. The string constant contains
  125. eight characters and looks like <code>&quot;23:59:01&quot;</code>.
  126. </p>
  127. <p>If GCC cannot determine the current time, it will emit a warning message
  128. (once per compilation) and <code>__TIME__</code> will expand to
  129. <code>&quot;??:??:??&quot;</code>.
  130. </p>
  131. </dd>
  132. <dt><code>__STDC__</code></dt>
  133. <dd><p>In normal operation, this macro expands to the constant 1, to signify
  134. that this compiler conforms to ISO Standard C. If GNU CPP is used with
  135. a compiler other than GCC, this is not necessarily true; however, the
  136. preprocessor always conforms to the standard unless the
  137. <samp>-traditional-cpp</samp> option is used.
  138. </p>
  139. <p>This macro is not defined if the <samp>-traditional-cpp</samp> option is used.
  140. </p>
  141. <p>On some hosts, the system compiler uses a different convention, where
  142. <code>__STDC__</code> is normally 0, but is 1 if the user specifies strict
  143. conformance to the C Standard. CPP follows the host convention when
  144. processing system header files, but when processing user files
  145. <code>__STDC__</code> is always 1. This has been reported to cause problems;
  146. for instance, some versions of Solaris provide X Windows headers that
  147. expect <code>__STDC__</code> to be either undefined or 1. See <a href="Invocation.html">Invocation</a>.
  148. </p>
  149. </dd>
  150. <dt><code>__STDC_VERSION__</code></dt>
  151. <dd><p>This macro expands to the C Standard&rsquo;s version number, a long integer
  152. constant of the form <code><var>yyyy</var><var>mm</var>L</code> where <var>yyyy</var> and
  153. <var>mm</var> are the year and month of the Standard version. This signifies
  154. which version of the C Standard the compiler conforms to. Like
  155. <code>__STDC__</code>, this is not necessarily accurate for the entire
  156. implementation, unless GNU CPP is being used with GCC.
  157. </p>
  158. <p>The value <code>199409L</code> signifies the 1989 C standard as amended in
  159. 1994, which is the current default; the value <code>199901L</code> signifies
  160. the 1999 revision of the C standard; the value <code>201112L</code>
  161. signifies the 2011 revision of the C standard; the value
  162. <code>201710L</code> signifies the 2017 revision of the C standard (which is
  163. otherwise identical to the 2011 version apart from correction of
  164. defects). An unspecified value larger than <code>201710L</code> is used for
  165. the experimental <samp>-std=c2x</samp> and <samp>-std=gnu2x</samp> modes.
  166. </p>
  167. <p>This macro is not defined if the <samp>-traditional-cpp</samp> option is
  168. used, nor when compiling C++ or Objective-C.
  169. </p>
  170. </dd>
  171. <dt><code>__STDC_HOSTED__</code></dt>
  172. <dd><p>This macro is defined, with value 1, if the compiler&rsquo;s target is a
  173. <em>hosted environment</em>. A hosted environment has the complete
  174. facilities of the standard C library available.
  175. </p>
  176. </dd>
  177. <dt><code>__cplusplus</code></dt>
  178. <dd><p>This macro is defined when the C++ compiler is in use. You can use
  179. <code>__cplusplus</code> to test whether a header is compiled by a C compiler
  180. or a C++ compiler. This macro is similar to <code>__STDC_VERSION__</code>, in
  181. that it expands to a version number. Depending on the language standard
  182. selected, the value of the macro is
  183. <code>199711L</code> for the 1998 C++ standard,
  184. <code>201103L</code> for the 2011 C++ standard,
  185. <code>201402L</code> for the 2014 C++ standard,
  186. <code>201703L</code> for the 2017 C++ standard,
  187. <code>202002L</code> for the 2020 C++ standard,
  188. or an unspecified value strictly larger than <code>202002L</code> for the
  189. experimental languages enabled by <samp>-std=c++23</samp> and
  190. <samp>-std=gnu++23</samp>.
  191. </p>
  192. </dd>
  193. <dt><code>__OBJC__</code></dt>
  194. <dd><p>This macro is defined, with value 1, when the Objective-C compiler is in
  195. use. You can use <code>__OBJC__</code> to test whether a header is compiled
  196. by a C compiler or an Objective-C compiler.
  197. </p>
  198. </dd>
  199. <dt><code>__ASSEMBLER__</code></dt>
  200. <dd><p>This macro is defined with value 1 when preprocessing assembly
  201. language.
  202. </p>
  203. </dd>
  204. </dl>
  205. <hr>
  206. <div class="header">
  207. <p>
  208. Next: <a href="Common-Predefined-Macros.html" accesskey="n" rel="next">Common Predefined Macros</a>, Up: <a href="Predefined-Macros.html" accesskey="u" rel="up">Predefined Macros</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index-of-Directives.html" title="Index" rel="index">Index</a>]</p>
  209. </div>
  210. </body>
  211. </html>