Standard-Predefined-Macros.html 11 KB

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